使用复合键调用 EnsureIndex 会导致索引对象中的 _id 字段
当我从 mongo shell 对复合索引的集合调用 EnsureIndex 时,会在索引对象中自动生成 ObjectId 类型的 _id 字段。
> db.system.indexes.find();
{ "name" : "_id_", "ns" : "database.coll", "key" : { "_id" : 1 } }
{ "_id" : ObjectId("4ea78d66413e9b6a64c3e941"), "ns" : "database.coll", "key" : { "a.b" : 1, "a.c" : 1 }, "name" : "a.b_1_a.c_1" }
这很直观,因为集合中的所有文档都需要 _id 字段(甚至是 system.indexes,对吧?),但是当我检查 morphia 的 EnsureIndex 调用为同一集合生成的索引时*没有 _id 属性*。
查看 morphia 的源代码,很明显它调用了 shell 使用的相同代码,但由于某种原因(无论是我正在创建复合索引还是索引嵌入式文档或两者兼而有之),它们产生了不同的结果。谁能向我解释这种行为?
When I call ensureIndex from the mongo shell on a collection for a compound index an _id field of type ObjectId is auto-generated in the index object.
> db.system.indexes.find();
{ "name" : "_id_", "ns" : "database.coll", "key" : { "_id" : 1 } }
{ "_id" : ObjectId("4ea78d66413e9b6a64c3e941"), "ns" : "database.coll", "key" : { "a.b" : 1, "a.c" : 1 }, "name" : "a.b_1_a.c_1" }
This makes intuitive sense as all documents in a collection need an _id field (even system.indexes, right?), but when I check the indexes generated by morphia's ensureIndex call for the same collection *there is no _id property*.
Looking at morphia's source code, it's clear that it's calling the same code that the shell uses, but for some reason (whether it's the fact that I'm creating a compound index or indexing an Embedded document or both) they produce different results. Can anyone explain this behavior to me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不完全确定您如何设法在索引集合中获取 _id 字段,但 shell 和 Morphia 发起的对复合索引的 EnsureIndex 调用不会将 _id 字段放入索引对象中:
如果您运行的是旧版本,请升级到 2.x以避免遇到现已解决的问题。从你的输出来看,你正在运行 1.8 或更早版本。
Not exactly sure how you managed to get an _id field in the indexes collection but both shell and Morphia originated ensureIndex calls for compound indexes do not put an _id field in the index object :
Upgrade to 2.x if you're running an older version to avoid running into now resolved issues. And judging from your output you are running 1.8 or earlier.