find Azure DocumentDB 中的指令用於查詢集合內的文件。 此指令是資料檢索操作的基礎,並可透過篩選、投影及查詢選項自訂,以微調結果。
語法
指令 find 的基本語法如下:
db.collection.find(query, projection, options)
參數
| 參數 | Description |
|---|---|
query |
一份指定文件取回條件的文件 |
projection |
(可選)一份文件,指定在結果集中要回傳的匹配文件欄位 |
options |
(可選)一份指定查詢行為與結果選項的文件 |
範例
請參考 StoreData 資料庫中 stores collection 的這份範例文件。
{
"_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:對物件進行查詢篩選器檢索文件
在位置物件的緯度與經度欄位使用查詢篩選器檢索文件。
db.stores.find({"location.lat": 13.5236, "location.lon": -82.5707})
當不使用 dot (.) 符號來參考物件內的欄位時,查詢過濾器應該完全符合整個物件,包括欄位的順序。
db.stores.find({"location": {"lat": 13.5236, "lon": -82.5707}})
範例 4:在陣列上使用查詢過濾器檢索文件
從 promotionEvents 陣列中取得文件,eventName 為「Grand Bargain Gala」。
db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"})
從「discounts」陣列中檢索文件,該陣列巢狀於 promotionEvents 陣列中,其中 categoryName 為「Area Rugs」。
db.stores.find({"promotionEvents.discounts.categoryName": "Area Rugs"})
預測
find 指令中的第二個文件指定了回應中要投射的欄位清單。
在回應中包含特定欄位或多個欄位
非零整數值或布林值為真,則該欄位包含在回應中。
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
}
]
}
}
在回應中排除特定欄位或多個欄位
整數值為零或布林值為假值則會排除該欄位。
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})
其中一份文件僅顯示 promotionEvents 陣列中活動名稱為「Grand Bargain Gala」的第一個元素,並排除陣列中其他所有元素。
{
"_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
}
}
}
]
}