Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道某些推荐需要订购 hashmap / 字典,但是 MongoDB 中的实际 BSON 文档是否重要,索引是否仍然有效?
例如
db.people.ensureIndex({LName:1, FName:1});
它是否适用于两者:
{LName:"abc", FName:"def"}, {FName:"ghi", LName:"jkl"}
?
谢谢
文档属性的顺序不影响索引。
您可以通过运行以下查询自己查看:
db.people.find({LName: "abc"}).explain()
然后这个查询:
db.people.find({LName: "jkl"}).explain()
您应该看到 MongoDB 在这两种情况下都会使用索引(cursor属性应该类似于"BtreeCursor LName_1_FName_1")。
cursor
"BtreeCursor LName_1_FName_1"