ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [MongoDB] MongoDB - 비교 연산자(Comparison Operator)
    NoSQL/MongoDB 2022. 7. 4. 18:42
    반응형

    비교 연산자는 어떤 값보다 크고 작은지 또는 같고 다른지에 대한 계산을 하는 연산자입니다.

     

    Example Collection

    db.products.insertMany([
    	{ "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]},
        { "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]},
        { "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]},
        { "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]},
        { "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]}
    ])

     

    1. $eq: Equal To Operator

    지정된 값과 같은 값을 가진 도큐먼트를 조회합니다.

    { field: { $eq: <value> } }

    예제: price 값이 899인 도큐먼트를 조회

    db.getCollection('products').find({'price': {$eq: 899}}) 또는 db.getCollection('products').find({'price': 899})


    2. $gt: Greater Than Operator

    지정된 값보다 큰 값을 가진 도큐먼트를 조회합니다.

    { field: { $gt: <value> } }

    예제: price 값이 799보다 큰 도큐먼트를 조회

    db.getCollection('products').find({'price': {$gt: 799}})

     

    3. $gte: Greater Than or Equal To Operator

    지정된 값보다 크거나 같은 값을 가진 도큐먼트를 조회합니다.

    { field: { $gte: <value> } }

    예제: price 값이 799보다 크거나 같은 도큐먼트를 조회

    db.getCollection('products').find({'price': {$gte: 799}})

     

    4. $lt: Less Than Operator

    지정된 값보다 작은 값을 조회합니다.

    { field: { $lt: value } }

    예제: price 값이 799보다 작은 도큐먼트를 조회

    db.getCollection('products').find({'price': {$lt: 799}})

     

    5. $lte: Less Than or Equal To Operator

    지정된 값보다 작거나 같은 값을 가진 도큐먼트를 조회합니다.

    { field: { $lte: value } }

    예제: price 값이 799보다 작거나 같은 도큐먼트를 조회

    db.getCollection('products').find({'price': {$lte: 799}})

     

    6. $ne: Not Equal To Operator

    지정된 값과 같지 않은 값을 가진 도큐먼트를 조회합니다.

    { field: { $ne: value } }

    예제: price 값이 899가 아닌 도큐먼트를 조회

    db.getCollection('products').find({'price': {$ne: 899}})

     

    7. $in: In Operator

    배열에 지정된 값과 같은 값을 가진 도큐먼트를 조회합니다.

    { field: { $in: [<value1>, <value2>, ... <valueN> ] } }

    예제: price 값이 699, 799인 도큐먼트를 조회

    db.getCollection('products').find({'price': {$in: [699, 799]}})


    8. $nin: Not In Operator

    배열에 지정된 값과 같지 않은 값을 가진 도큐먼트를 조회합니다.

    { field: { $nin: [ <value1>, <value2> ... <valueN> ] } }

    예제: price 값이 699, 799가 아닌 도큐먼트를 조회

    db.getCollection('products').find({'price': {$nin: [699, 799]}})


    참고 : https://www.mongodb.com/docs/manual/reference/operator/query/
    참고 : https://www.mongodbtutorial.org/mongodb-crud/

    반응형

    댓글

Designed by Tistory.