mongodb mongo中的子文档索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16769705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:14:44  来源:igfitidea点击:

Subdocument index in mongo

mongodbindexing

提问by slezadav

What exactly happens when I call ensureIndex(data)when typical data looks like data:{name: "A",age:"B", job : "C"}? Will it create a compound index over these three fields or will it create only one index applicable when anything from data is requested or something altogether different ?

当我打电话给ensureIndex(data)典型数据时究竟会发生什么data:{name: "A",age:"B", job : "C"}?它会在这三个字段上创建一个复合索引,还是只创建一个适用于请求数据中的任何内容或完全不同的索引?

回答by jimoleary

You can do either :

您可以执行以下任一操作:

> db.collection.ensureIndex({"data.name": 1,"data.age":1, "data.job" : 1})
> db.collection.ensureIndex({"data": 1})

This is discussed in the documentation under indexes-on-embedded-fieldsand indexes on sub documents

这在索引嵌入字段子文档索引下的文档中进行了讨论

The important section of the sub document section is 'When performing equality matches on subdocuments, field order matters and the subdocuments must match exactly.'

子文档部分的重要部分是“对子文档执行相等匹配时,字段顺序很重要,子文档必须完全匹配”。

This means that the 2 indexes are the same for simple queries .

这意味着 2 个索引对于简单查询是相同的。

However, as the sub-document example shows, you can get some interesting results (that you might not expect) if you just index the whole sub-document as opposed to a specific field and then do a comparison operator (like $gte) - if you index a specific sub field you get a less flexible, but potentially more useful index.

但是,正如子文档示例所示,如果您只是索引整个子文档而不是特定字段,然后执行比较运算符(如$gte),您可以获得一些有趣的结果(您可能不会想到)索引一个特定的子字段,你会得到一个不太灵活但可能更有用的索引。

It really all depends on your use case.

这真的完全取决于您的用例。

Anyway, once you have created the index you can check what's created with :

无论如何,一旦您创建了索引,您就可以检查创建的内容:

> db.collection.getIndexes()
[
{
    "v" : 1,
    "key" : {
        "_id" : 1
    },
    "ns" : "test.collection",
    "name" : "_id_"
},
{
    "v" : 1,
    "key" : {
        "data.name" : 1,
        "data.age" : 1,
        "data.job" : 1
    },
    "ns" : "test.collection",
    "name" : "data.name_1_data.age_1_data.job_1"
}

]

]

As you can see from the output it created a new key called data.name_1_data.age_1_data.job_1(the _id_index is always created).

正如您从输出中看到的那样,它创建了一个名为的新键data.name_1_data.age_1_data.job_1_id_始终创建索引)。

If you want to test your new index then you can do :

如果你想测试你的新索引,那么你可以这样做:

> db.collection.insert({data:{name: "A",age:"B", job : "C"}})
> db.collection.insert({data:{name: "A1",age:"B", job : "C"}})
> db.collection.find({"data.name" : "A"}).explain()
{
    "cursor" : "BtreeCursor data.name_1_data.age_1_data.job_1",
     .... more stuff

The main thing is that you can see that your new index was used (BtreeCursor data.name_1_data.age_1_data.job_1in the cursor field is what indicates this is the case). If you see "cursor" : "BasicCursor", then your index was not used.

最重要的是你可以看到你的新索引被使用了(游标字段中的BtreeCursor data.name_1_data.age_1_data.job_1表明是这种情况)。如果您看到"cursor" : "BasicCursor",那么您的索引未被使用。

For more detailed information look here.

有关更多详细信息,请查看此处

回答by Diwakar upadhyay

you can try this :

你可以试试这个:

db.collection.ensureIndex({"data.name": 1,"data.age":1, "data.job" : 1})