18

http://www.mongodb.org/display/DOCS/Indexesのインデックス ヘルプ ページでは $elemMatch について言及されておらず、2M 以上のオブジェクト コレクションにインデックスを追加するように指示されているため、次のように尋ねてみました。

私は次のようなクエリを実行しています:

{ lc: "eng", group: "xyz", indices: { $elemMatch: { text: "as", pos: { $gt: 1 } } } }

インデックスを追加すると

{lc:1, group:1, indices.text:1, indices.pos:1}

$elemMatch コンポーネントを使用したこのクエリは、インデックスを完全に実行できますか?

4

1 に答える 1

25

クエリに基づいて、ドキュメントは次のようになると思います。

{
    "_id" : 1,
    "lc" : "eng",
    "group" : "xyz",
    "indices" : [
        {
            "text" : "as",
            "pos" : 2
        }, 
        {
            "text" : "text",
            "pos" : 4
        }
    ]
}

この形式のドキュメントでテスト コレクションを作成し、インデックスを作成し、.explain() オプションを使用して投稿したクエリを実行しました。

インデックスは期待どおりに使用されています。

> db.test.ensureIndex({"lc":1, "group":1, "indices.text":1, "indices.pos":1})
> db.test.find({ lc: "eng", group: "xyz", indices: { $elemMatch: { text: "as", pos: { $gt: 1 } } } }).explain()
{
    "cursor" : "BtreeCursor lc_1_group_1_indices.text_1_indices.pos_1",
    "isMultiKey" : true,
    "n" : NumberLong(1),
    "nscannedObjects" : NumberLong(1),
    "nscanned" : NumberLong(1),
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : NumberLong(0),
    "millis" : 0,
    "indexBounds" : {
        "lc" : [
            [
                "eng",
                "eng"
            ]
        ],
        "group" : [
            [
                "xyz",
                "xyz"
            ]
        ],
        "indices.text" : [
            [
                "as",
                "as"
            ]
        ],
        "indices.pos" : [
            [
                {
                    "$minElement" : 1
                },
                {
                    "$maxElement" : 1
                }
            ]
        ]
    },
    "server" : "Marcs-MacBook-Pro.local:27017"
}

.explain() 機能に関するドキュメントは、http ://www.mongodb.org/display/DOCS/Explain にあります。

.explain() は、使用されているインデックス (存在する場合) など、クエリに関する情報を表示するために使用できます。

于 2012-04-18T16:58:56.463 に答える