MongoDB 的 docs解释多键索引。考虑这个评论
文档。
{
"_id": ObjectId(...)
"title": "Grocery Quality"
"comments": [
{ author_id: ObjectId(...)
date: Date(...)
text: "Please expand the cheddar selection." },
{ author_id: ObjectId(...)
date: Date(...)
text: "Please expand the mustard selection." },
{ author_id: ObjectId(...)
date: Date(...)
text: "Please expand the olive selection." }
]
}
文档解释说可以在 comments.text
或任何 comments
' 字段上建立索引。 但是,是否可以对 comments
键本身建立索引?
这个post演示了对字符串数组进行索引,但是,上面的 comments
字段是 JSON 对象数组。
基于 Antoine Girbal 的 article ,似乎可以对 JSON 对象数组进行索引,其中每个 JSON 对象都有一个不同键名称。但是,数组中的每个 JSON 对象共享相同的键名称似乎是不可能的。
最佳答案
是的,可以index subdocuments它们可以位于多键索引中。当索引整个子文档时,它仅在搜索整个文档时才会匹配,例如:
db.test.find({records: {hair: "brown"}})
搜索与 {hair: "brown"}
完全匹配的文档的记录
,并且可以使用索引来查找它。
如果您想查找任何包含 hair="brown"
和任何其他字段的子文档 dot notation需要例如:
db.test.find({"records.hair": "brown"})
但是,没有可用的索引 - 因此它是全表扫描。
请注意:index size 有限制整个文档很容易超过该大小。
关于mongodb - 整个数组上的多键索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124536/