1. 비교 연산자
비교 연산자에 대해 알아보기 위해 snacks라는 컬렉션을 생성하고 문서를 넣도록 하겠습니다.
db.snacks.insertMany([ { _id : 301, "item" : "saewookkang", "qty" : 27, "price" : 1200, "taste" : [ "black", "red" ], "company" : "nongshim", "Date" : { "MPD" : ISODate("2019-12-31"), "EXP" : ISODate("2021-03-31") } }, { _id : 302, "item" : "pokachip", "qty" : 4, "price" : 1700, "taste" : [ "original", "onion" ], "company" : "orion", "Date" : { "MPD" : ISODate("2021-08-10"), "EXP" : ISODate("2022-02-11") } }, { _id : 303, "item" : "pepero", "qty" : 12, "price" : 1200, "taste" : [ "original", "yogurt", "choco_cookie" ], "company" : "lotte", "Date" : { "MPD" : ISODate("2021-10-25"), "EXP" : ISODate("2022-01-03") } }, { _id : 304, "item" : "chocopie", "qty" : 33, "price" : 2700, "taste" : [ "original", "injeolmi", "strewberry" ], "company" : "nongshim", "Date" : { "MPD" : ISODate("2019-11-01"), "EXP" : ISODate("2021-03-09") } }, { _id : 305, "item" : "cheetos", "qty" : 23, "price" : 1000, "taste" : [ "spicy", "sweet" ], "company" : "lotte", "Date" : { "MPD" : ISODate("2019-12-25"), "EXP" : ISODate("2021-05-18") } } ])

비교 연산자의 종류는 다음과 같습니다.
- eq(Equality) : 해당 값과 일치하는 문서 검색합니다.
db.snacks.find({company:{$eq:"lotte"}})

eq는 find로 검색할 때와 출력 결과가 같습니다.
db.snacks.find({company:"lotte"})

날짜 검색하는 방법과 함께 중첩된 문서의 필드가 같은지 검색해 보도록 하겠습니다. 날짜 검색은 ISODate("yyyy-mm-dd")를 사용하시면 됩니다. 중첩된 문서의 필드를 검색할 때에는 점 표기법을 사용합니다.
db.snacks.find({"Date.MPD":{$eq : ISODate("2019-12-25")}})

db.snacks.find({"Date.MPD" : ISODate("2019-12-25")})

배열 요소가 값과 같은지 검색합니다.
db.snacks.find({"taste":{$eq:"original"}})

db.snacks.find({"taste":"original"})

- lt(Less Than) : 미만
db.snacks.find( { "qty": { $lt: 20 } } )

- lte(Less Than Equals) : 이하
db.user_info.find( { "size.h": { $lte: 165 } } )
- gt(Greater Than) : 초과
db.user_info.find( { age: { $gt: 25 } } )
- gte(Greater Than Equals) : 이상
db.user_info.find( { age: { $gte: 25 } } )
- in(Valuse in an array) : 값을 포함
동일한 필드를 검색할 때, or 연산자도 사용하지만 in 연산자도 사용합니다.
db.user_info.find({ABO:{$in:["O","B"]}})
- nin(Valuse not in an array) : 값이 nin 안에 값들이 아닌 문서 검색
db.user_info.find({ABO:{$nin:["O","B"]}})
- ne(Not Equals) : 해당값과 일치하지 않는 문서 검색
db.user_info.find({ABO:{$ne:"O"}})
2. 논리 연산자
AND 및 OR 논리를 사용하여 데이터를 읽어 복합쿼리를 구성할 수 있습니다.
모든 쿼리와 일치하는 복합 쿼리를 MongoDB에 작성하려면 찾기 문서에서 일치시키려는 모든 필드를 지정하셔야 합니다. 기본적으로 MongoDB는 모든 필드와 일치합니다.
db.user_info.find( {$and:[{ABO: "B"}, {age: { $lt: 20 } } ] } )

MongoDB는 또한 AND 논리 연산자의 일부로 논리 연산자를 제공하지만 위에서 설명한 암시적 AND가 더 일반적인 패턴입니다.
db.user_info.find( {ABO: "B", age: { $lt: 20 } } )
db.user_info.find( {$and:[{ABO: "B"}, {age: { $lt: 20 } } ] } )
OR 연산자를 사용하여 컬렉션에서 하나 이상의 조건과 일치하는 문서를 선택하도록 각 절을 논리적 연결로 조인하는 복합 쿼리를 지정합니다 .
db.user_info.find( {$or:[{ABO: "A"}, {"size.w":{ $lt: 55 }}]})

이제 AND 및 OR 논리를 함께 사용하여 MongoDB에서 데이터를 검색하여 복합 쿼리를 구성합니다.
db.user_info.find({ABO:"B", $or:[{age: { $lt: 25 }},{name:/i/}]})

not은 조건을 만족하지 않는 문서를 검색합니다.
db.user_info.find({age: { $not: { $lt: 30 } } } )

nor은 조건이 하나라도 만족하지 않는 문서를 검색합니다.
db.user_info.find({ $nor: [ {age: { $lt: 30 } }, {gender : "F"} ] } )

3. 요소 연산자
goods라는 컬렉션을 생성해보도록 하겠습니다.
db.goods.insertMany([ {item:"pencil", price:500, pre_price:300, qty:10, shape:["circle","triangle"]}, {item:"notebook", price:1000, pre_price:1200, qty:2, type:["line","no-line"]}, {item:"eraser", price:300, pre_price:500, qty:13}, {item: 123, price:700, pre_price:500, qty:20} ])
- exists : 해당 필드가 존재하는지 않은지 판단 후 출력합니다. 필드가 존재할 경우 1, 존재하지 않을 경우 0을 입력합니다.
db.goods.find({shape:{$exists:1}})

- type : 해당필드의 자료형이일치한 문서를 검색
db.goods.find({item:{$type:"string"}})

4. 정렬 연산자
sort를 이용하여 1은 오름차순 -1은 내림차순으로 정렬할 수 있습니다.
db.snacks.find().sort({price:1})

db.snacks.find().sort({price:-1})

정렬 순서를 지정해줄 수 있습니다.
db.snacks.find().sort({price:-1,name:1})

5. 제한 연산자
limit은 문서 위에서 부터 값 만큼의 데이터를 불러옵니다.
db.snacks.find().limit(값)

skip은 문서 뒤에서 부터 값 만큼의 데이터를 불러옵니다.
db.snacks.find().skip(값)

6. 배열 연산자
중첩된 문서에서 조건과 동일한 문서를 검색합니다. 이때, 문서와 일치하는 모든 필드를 검색하려면 컬렉션에 나타나는 순서대로 포함된 문서의 모든 필드와 값이 일치해야 합니다.
db.user_info.find( { size: { h: 161, w: 55 } )

객체의 필드 중 하나와 정확히 일치하는 문서를 검색하려면 점 표기법을 사용합니다. 이때, 필드 이름은 따옴표 안에 있어야 합니다.
db.user_info.find({"size.w" : 50})

특정 인덱스 위치로 검색하는 방법은 점 표기법을 사용하여 검색할 수 있습니다. 배열은 0부터 시작하는 인덱싱을 사용합니다. 점 표기법을 사용할 때에는 필드는 따옴표 안에 있어야 합니다.
다음 예제에서는 배열의 두 번째 요소
dim_cm
가 다음보다 큰 모든 문서를 쿼리합니다 25
.db.inventory.find( {"dim_cm.1": { $gt: 25 } } )
배열 검색을 공부하기 위해 snacks라는 컬렉션을 생성하도록 하겠습니다.
db.snacks.insertMany([ { item: "saewookkang", price: 1200, taste: ["black", "red"], company: "nongshim" }, { item: "pokachip", price: 1700, taste: ["original", "onion"], company: "orion" }, { item: "pepero", price: 1200, taste: ["original", "almond", "crunch"], company: "lotte" }, { item: "chocopie", price: 2700, taste: ["original", "injeolmi","strewberry"], company: "nongshim" }, { item: "cheetos", price: 1000, taste: ["spicy","sweet"], company: "lotte" } ]);

배열 조건을 지정하려면, 지정된 순서로 포함된 배열인 모든 문서를 검색합니다.
db.snacks.find( { taste: ["red", "black"] } )

배열의 순서나 다른 요소에 관계없이 요소 모두를 포함하는 배열을 찾으려면 all연산자를 사용합니다.
db.snacks.find( { taste: { $all: ["black", "red"] } } )

조건에 배열 필드가 포함되어 있는 경우, 적어도 하나개의 지정된 값을 가진 문서를 검색합니다.
db.snacks.find( { taste: "red" } )

배열에 복합 조건을 지정할 때 단일 배열 요소가 조건을 충족하거나 배열의 조합이 조건을 충족하도록 쿼리를 지정할 수 있습니다.
db.snacks.find({$nor:[{"price": {$lt: 1000}}, {"company":"lotte"}]})
