$ elemMatch的MongoDB索引 [英] MongoDB indexes for $elemMatch

查看:426
本文介绍了$ elemMatch的MongoDB索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.mongodb.org/display/DOCS/Indexes上的索引帮助页面没有提到$ elemMatch,因为在我的2M +对象集合上添加索引的天数,我想我会问:

The indexes help page at http://www.mongodb.org/display/DOCS/Indexes doesn't mention $elemMatch and since it days days to add an index on my 2M+ object collection I thought I'd ask this:

我在做什么如下查询:

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

如果我添加索引

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

这个带有$ elemMatch组件的查询是否可以通过索引完全运行?

will this query with the $elemMatch component be able to be fully run through the index?

推荐答案

根据您的查询,我想您的文档看起来像这样:

Based on your query, I imagine that your documents look something like this:

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

我创建了一个包含此格式文档的测试集合,创建了索引,并运行了您使用.explain()选项发布的查询。

I created a test collection with documents of this format, created the index, and ran the query that you posted with the .explain() option.

索引按预期使用:

> 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

The documentation on the .explain() feature may be found here: http://www.mongodb.org/display/DOCS/Explain

.explain()可用于显示有关查询的信息,包括使用哪个(如果有)索引。

.explain() may be used to display information about a query, including which (if any) index is used.

这篇关于$ elemMatch的MongoDB索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆