Azure DocumentDB의 find 명령은 컬렉션 내의 문서를 쿼리하는 데 사용됩니다. 이 명령은 데이터 검색 작업의 기본 사항이며 필터, 프로젝션 및 쿼리 옵션을 사용하여 사용자 지정하여 결과를 미세 조정할 수 있습니다.
문법
명령에 대한 find 기본 구문은 다음과 같습니다.
db.collection.find(query, projection, options)
매개 변수
| 매개 변수 | Description |
|---|---|
query |
검색할 문서의 조건을 지정하는 문서 |
projection |
(선택 사항) 결과 집합에서 반환할 일치하는 문서의 필드를 지정하는 문서입니다. |
options |
(선택 사항) 쿼리 동작 및 결과에 대한 옵션을 지정하는 문서 |
예제(들)
StoreData 데이터베이스의 Store 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
예제 1: 모든 문서 검색
쿼리 필터가 없는 find() 명령은 컬렉션의 모든 문서를 반환합니다.
db.stores.find()
예제 2: 쿼리 필터를 사용하여 문서 검색
이름 속성에 대한 필터를 사용하여 문서를 검색합니다.
db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"})
예제 3: 개체에 대한 쿼리 필터가 있는 문서 검색
위치 개체 내의 위도 및 lon 필드에서 쿼리 필터를 사용하여 문서를 검색합니다.
db.stores.find({"location.lat": 13.5236, "location.lon": -82.5707})
점(.) 표기법을 사용하여 개체 내의 필드를 참조하지 않는 경우 쿼리 필터는 필드 순서를 포함하여 전체 개체와 정확히 일치해야 합니다.
db.stores.find({"location": {"lat": 13.5236, "lon": -82.5707}})
예제 4: 배열에서 쿼리 필터가 있는 문서 검색
eventName이 "Grand Bargain Gala"인 promotionEvents 배열에서 문서를 검색합니다.
db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"})
categoryName이 "Area Rugs"인 promotionEvents 배열 내에 중첩된 "할인" 배열에서 문서를 검색합니다.
db.stores.find({"promotionEvents.discounts.categoryName": "Area Rugs"})
예측
찾기 명령의 두 번째 문서는 응답에서 프로젝트할 필드 목록을 지정합니다.
응답에 특정 필드 또는 여러 필드 포함
0이 아닌 정수 값 또는 true의 부울 값은 응답에 필드를 포함합니다.
db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"location": 1, "sales": 1})
db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"location": true, "sales": true})
db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"location": 1, "sales": true})
db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"location": 1, "sales": -5})
네 개의 쿼리는 모두 동일하며 서버 응답에 "위치" 및 "판매" 필드를 포함할지 지정합니다.
{
"_id": "b5c9f932-4efa-49fd-86ba-b35624d80d95",
"location": {
"lat": 13.5236,
"lon": -82.5707
},
"sales": {
"totalSales": 35346,
"salesByCategory": [
{
"categoryName": "Rulers",
"totalSales": 35346
}
]
}
}
응답에서 특정 필드 또는 여러 필드 제외
정수 값이 0이거나 부울 값이 false이면 지정된 필드가 쿼리 응답에서 제외됩니다.
db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"promotionEvents": 0, "location": 0, "sales": 0})
db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"}, {"promotionEvents": false, "location": false, "sales": false})
두 쿼리 모두 동일하며 다음 응답을 반환합니다.
{
"_id": "b5c9f932-4efa-49fd-86ba-b35624d80d95",
"name": "Fourth Coffee | Stationery Haven - New Franco",
"staff": {
"totalStaff": {
"fullTime": 17,
"partTime": 5
}
}
}
비고
기본적으로 _id 필드는 서버 응답에 포함됩니다. 프로젝션 문서에 포함 절과 제외 절을 모두 포함할 수 없습니다. 그러나 _id 필드는 이 규칙의 유일한 예외이며 포함할 필드 목록과 함께 제외할 수 있으며 그 반대의 경우도 마찬가지입니다.
쿼리 필터 조건과 일치하는 배열의 첫 번째 요소 프로젝션
"arrayFieldName".$ 명령은 지정된 쿼리 필터와 일치하는 배열에서 개체의 첫 번째 항목만 투영합니다.
db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"}, {"promotionEvents.$": true})
반환된 문서 중 하나에는 이벤트 이름이 "Grand Bargain Gala"인 promotionEvents 배열의 첫 번째 요소만 표시되며 배열의 다른 모든 요소는 제외됩니다.
{
"_id": "d7fe6fb9-57e8-471a-b8d2-714e3579a415",
"promotionEvents": [
{
"eventName": "Grand Bargain Gala",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 25
},
"endDate": {
"Year": 2024,
"Month": 4,
"Day": 1
}
},
"discounts": [
{
"categoryName": "Area Rugs",
"discountPercentage": 7
},
{
"categoryName": "Vinyl Flooring",
"discountPercentage": 12
}
]
}
]
}
쿼리 필터 조건과 일치하는 배열의 프로젝트별 요소
이 쿼리는 promotionEvents 배열 내에 eventName 속성 및 중첩된 Year 속성을 프로젝션합니다.
db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"}, {"promotionEvents.eventName": true, "promotionEvents.promotionalDates.startDate.Year": true})
반환된 문서 중 하나는 응답에 프로젝션된 지정된 배열 요소를 보여줍니다.
{
"_id": "d7fe6fb9-57e8-471a-b8d2-714e3579a415",
"promotionEvents": [
{
"eventName": "Grand Bargain Gala",
"promotionalDates": {
"startDate": {
"Year": 2024
}
}
},
{
"eventName": "Grand Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024
}
}
},
{
"eventName": "Epic Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024
}
}
}
]
}