密钥顺序在MongoDB BSON文档中有关系吗? [英] Does Key order matter in a MongoDB BSON doc?

查看:49
本文介绍了密钥顺序在MongoDB BSON文档中有关系吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道某些建议需要对哈希图/字典进行排序,但是MongoDB中的实际BSON文档是否重要,索引是否仍然有效?

I know certain commends need the hashmap / dictionary to be ordered, but does the actual BSON document in MongoDB matter and would the index still work?

例如

db.people.ensureIndex({LName:1, FName:1});

这两种方法都可以使用吗?

Would it work on both:

{LName:"abc", FName:"def"}, 
{FName:"ghi", LName:"jkl"} 

?

谢谢

推荐答案

文档属性的顺序不影响索引编制.

The order of a document's properties does not affect indexing.

您可以通过运行以下查询来亲自查看:

You can see this for yourself by running this query:

db.people.find({LName: "abc"}).explain()

,然后执行以下查询:

db.people.find({LName: "jkl"}).explain()

您应该看到在两种情况下MongoDB都将使用索引(cursor属性应该类似于"BtreeCursor LName_1_FName_1").

you should see that MongoDB will use the index in both cases (the cursor property should be something like "BtreeCursor LName_1_FName_1").

这篇关于密钥顺序在MongoDB BSON文档中有关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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