mongo中的子文档索引 [英] Subdocument index in mongo

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

问题描述

当典型数据看起来像数据时,当我调用 ensureIndex(data)时会发生什么?{name:A,age:B ,工作:C} ?它会在这三个字段上创建一个复合索引,还是只创建一个索引适用于请求数据的东西或完全不同的东西?

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 ?

推荐答案

你可以这样做:

> 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-fields and 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.'

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

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

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

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.

这完全取决于你的用例。

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

]

从输出中可以看出,它创建了一个名为 data.name_1_data的新密钥.age_1_data.job_1 (始终创建 _id _ 索引。)

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

如果你想测试你的新索引然后你可以做:

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

主要的是你可以看到你的新索引被使用了( 光标字段中的BtreeCursor data.name_1_data.age_1_data.job_1 表示情况就是这样)。如果您看到cursor:BasicCursor,那么您的索引未被使用。

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

更多详细信息请查看此处

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

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