ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [MongoDB] MongoDB - 논리 연산자(Logical Operator)
    NoSQL/MongoDB 2022. 7. 5. 15:16
    반응형

    논리 연산자는 쿼리안의 조건들의 논리적 연결을 위해서 사용되며, 단독으로는 존재할 수 없고, 다른 연산자와 연계해서 사용합니다.

     

    Example Collection

    db.products.insertMany([
    	{ "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate" : ISODate("2011-05-14T00:00:00Z"), "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-01T00:00:00Z"), "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-14T00:00:00Z"), "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-14T00:00:00Z"), "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-14T00:00:00Z"), "spec" : { "ram" : 4, "screen" : 9.7, "cpu" : 1.66 }, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256 ] },
    	{ "_id" : 6, "name" : "xWidget", "spec" : { "ram" : 64, "screen" : 9.7, "cpu" : 3.66 }, "color" : [ "black" ], "storage" : [ 1024 ] }
    ])

     

    1. $and

    주어진 모든 조건을 만족하는 도큐먼트를 조회합니다.

    { $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

    예제: price 값이 899이고, color 값이 'white', 'black'인 도큐먼트를 조회

    db.products.find({$and: [{price: 899}, {color: {$in: ["white", "black"]}}]})

     

    2. $or

    주어진 조건 중 하나라도 만족하는 도큐먼트를 조회합니다.

    { $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

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

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

     

    3. $not

    주어진 조건을 만족하지 않는 도큐먼트를 조회합니다.

    { field: { $not: { <operator-expression> } } }

    예제: price 값이 699 보다 크지 않은 도큐먼트를 조회

    db.products.find({price: {$not: {$gt: 699}}})

     

    4. $nor

    주어진 조건 중 하나라도 만족하지 않는 도큐먼트를 조회합니다.

    { $nor: [ { <expression1> }, { <expression2> }, ...  { <expressionN> } ] }

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

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

     

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

    반응형

    댓글

Designed by Tistory.