找到

find Azure Cosmos DB for MongoDB(vCore)中的命令用于查询集合中的文档。 此命令是数据检索作的基础,可以使用筛选器、投影和查询选项进行自定义以微调结果。

语法

find 命令的基本语法如下:

db.collection.find(query, projection, options)

参数

参数 说明
query 指定要检索的文档的条件的文档
projection (可选)一个文档,指定要在结果集中返回的匹配文档中的字段
options (可选)一个文档,指定查询行为和结果的选项

示例

请查看 StoreData 数据库中 stores 集合的这个示例文档。

{
    "_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:在对象上使用查询筛选器检索文档

使用位置对象内 lat 和 lon 字段上的查询筛选器检索文档。

db.stores.find({"location.lat": 13.5236, "location.lon": -82.5707})

当点(.)表示法不用于引用对象中的字段时,查询筛选器应与包括字段顺序在内的整个对象完全匹配。

db.stores.find({"location": {"lat": 13.5236, "lon": -82.5707}})

示例 4:检索包含数组上的查询筛选器的文档

从 eventName 为“大讨价还价晚会”的 promotionEvents 数组中检索文档。

db.stores.find({"promotionEvents.eventName": "Grand Bargain Gala"})

从“折扣”数组中检索文档,该数组嵌套在 categoryName 为“Area Rugs”的 promotionEvents 数组中。

db.stores.find({"promotionEvents.discounts.categoryName": "Area Rugs"})

预测

find 命令中的第二个文档指定要在响应中投影的字段列表。

在响应中包含特定字段或多个字段

非零整数值或 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})

这四个查询都是等效的,并指定在服务器响应中包含“location”和“sales”字段。

{
    "_id": "b5c9f932-4efa-49fd-86ba-b35624d80d95",
    "location": {
        "lat": 13.5236,
        "lon": -82.5707
    },
    "sales": {
        "totalSales": 35346,
        "salesByCategory": [
            {
                "categoryName": "Rulers",
                "totalSales": 35346
            }
        ]
    }
}

排除响应中的特定字段或多个字段

值为零或布尔值的整数值 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})

返回的文档之一仅显示 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
                }
            }
        }
    ]
}