次の方法で共有


find

Azure DocumentDB の find コマンドは、コレクション内のドキュメントに対してクエリを実行するために使用されます。 このコマンドはデータ取得操作の基本であり、フィルター、プロジェクション、およびクエリ オプションを使用してカスタマイズして結果を微調整できます。

構文

find コマンドの基本的な構文は次のとおりです。

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

パラメーター

パラメーター Description
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: クエリ フィルターを使用してドキュメントを取得する

name プロパティのフィルターを使用してドキュメントを取得します。

db.stores.find({"name": "Fourth Coffee | Stationery Haven - New Franco"})

例 3: オブジェクトに対するクエリ フィルターを使用してドキュメントを取得する

location オブジェクト内の 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 が "Grand Bargain Gala" である promotionEvents 配列からドキュメントを取得します。

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

categoryName が "Area Rugs" である promotionEvents 配列内に入れ子になっている "discounts" 配列からドキュメントを取得します。

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

予測

検索コマンドの 2 番目のドキュメントでは、応答で射出するフィールドの一覧を指定します。

応答に特定のフィールドまたは複数のフィールドを含める

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})

4 つのクエリはすべて同等であり、サーバー応答に "location" フィールドと "sales" フィールドを含めることを指定します。

{
    "_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})

返されるドキュメントの 1 つは、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})

返されるドキュメントの 1 つは、応答に投影された指定された配列要素を示しています。

{
    "_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
                }
            }
        }
    ]
}