반응형
Query embedded document
- {field : document}
- 순서가 다를 경우 검색이 안됨
- 모든 embedded document의 필드가 없으면 검색이 안됨
// 알맞은 쿼리
db.inventory.find({size: {h:14, w:21, uom: "cm"}})
// 틀린 쿼리
// 순서가 다르면 검색이 안된다.
db.inventory.find({size: {w:21, h:14, uom:"cm"}})
// 모두 매치되지 않으면, 검색이 안된다.
db.inventory.find({size: {h:14, w:21}})
- (”field.nestedField”)
// size.uom이 "in"인 값들을 찾는다.
db.inventory.find({"size.uom": "in"})
// size.h가 14이고, size.w가 21인 값들을 찾는다.
db.inventory.find({"size.h": 14, "size.w": 21})
Query an array
- { field : array }
// 순서까지 정확해야 정확한 검색이 된다. db.inventory2.find({ tags:["red", "blank"]})
- {field : array value}
db.inventory2.find({tags: "red"})
- dot notation을 사용하면 배열의 값에 접근할 수 있다.
- zero-based indexing(0부터 시작)
db.inventory2.find({"dim_cm.1":{$gt:25}}) db.inventory2.find({"tags.0":"blank", "dim_cm.1": {$gt:25}})
Query an array of documents
db.inventory3.find({"instock": {warehouse: "A", pty: 5}})
// 순서가 다르면 검색되지 않는다.
db.inventory3.find({"instock": {pty: 5, warehouse: "A"}})
- dot notaion 사용
db.inventory3.find({"instock.qty":{$gte:20}})
// 0번째 document의 qty가 5인 document 검색
db.inventory3.find({"instock.0.qty": 5})
Element Operators
- { field : null }
// item field의 값이 null이거나 없는 경우 db.inventory3.find({item:null}) // item field의 null이 아닌 경우 db.inventory3.find({item: {$ne: null}})
- $exist
- { field : {$exist : false}}
- 특정 필드를 가지고 있지 않은 document를 찾는다
db.inventory3.find({item:{$exists:false}}) // null을 포함해서 item field를 가지고 있는 document 출력 db.inventory3.find({item:{$exists:true}}, {_id:0,item:1})
- { field : {$exist : false}}
- $type
- { field: {$type: BSON type}}
db.grades.find({"classAverage": {$type: "string"}})
db.grades.find({"classAverage": {$type: 2}})
// double, 32-bit integer, 64-bit integer, decimal
db.grades.find({"classAverage": {$type: "number"}})
// string or double
db.grades.find({"classAverage": {$type: [2,1]}})
db.grades.find({"classAverage": {$type: ["string","double"]}})
Regular Expression
- $regex
- { field: {$regex: /pattern/, $options: “options”}}
- { field: {$regex: “pattern”, $options: “options”}}
- ^ and $
- ^: 특정 character로 시작하는 경우
- $: 특정 character로 끝나는 경우
- options: i
- 대소문자를 구분하지 않는다.
// paper를 포함한 document
db.inventory.find({item:{$regex:"paper"}})
// note로 시작하는 경우
db.inventory.find({item: {$regex: "^note"}}
db.inventory.find({item: {$regex: /^note/}}
// nal로 끝나는 경우
db.inventory.find({item: {$regex: "nal$"}})
db.inventory.find({item: {$regex: /nal$/}})
// 대소문자 구분 없이 PAPER를 포함한 경우
db.inventory.find({item: {$regex: "PAPER", $options: "i"}})
db.inventory.find({item: {$regex: /PAPER/, $options: "i"}})
Task
Task 1
Find the restaurants that do not prepare any cuisine of 'American' and their grade score more than 70
db.restaurants.find({"cuisine" : {$ne: "American"}, "grades.score": {$gt: 70}})
Task 2
Find the restaurants which prepare American or Chinese dish and achieved a score more than 60 and located in the latitude less than -74.
db.restaurants.find({"cuisine": {$in: ["American", "Chinese"]},
"grades.score": {$gt: 60}, "address.coord.0": {$lt: -74}})
Task 3
Find the restaurant Id, name, and scores for those restaurants where the eighth element of grades contains a score greater than 30
db.restaurants.find({"grades.7.score": {$gt: 30}},
{_id: 0 , "restaurant_id": 1, "name": 1, "grades.score": 1})
Task 4
Find the restaurants that are located either in Staten Island or Queens and contain ‘Wen' as first three letters for its name
db.restaurants.find( {"borough": {$in: ["Staten Island", "Queens"]}, name: {$regex : "^Wen"}})
Task 5
Find whether all the addresses contains the street or not
db.restaurants.find({ "address.street": {$exists: false}})
Task 6
Check if all address coordinate values are in an array format
db.restaurants.find({"address.coord": {$type: "array"}})
반응형
'Back-end > MongoDB' 카테고리의 다른 글
MongoDB: 고급 쿼리(Advanced Queries - Aggregation Framework) (0) | 2022.04.29 |
---|---|
MongoDB: 기본 쿼리(Basic Quries) (0) | 2022.04.29 |
MongoDB Overview (0) | 2022.04.26 |
Big Data Storage (0) | 2022.04.26 |
Introduction to Big Data (0) | 2022.04.26 |