【发布时间】:2012-02-22 05:29:44
【问题描述】:
多键的 mongodb 文档给出了查询数组中嵌入对象字段的示例:
http://www.mongodb.org/display/DOCS/Multikeys
但没有说明如何为这种情况创建索引。在数组上创建索引似乎不起作用(使用解释机制可以看到索引未使用)。
其他细节:
> // find posts where julie commented
> db.posts.find( { "comments.author" : "julie" } )
{"title" : "How the west was won",
"comments" : [{"text" : "great!" , "author" : "sam"},
{"text" : "ok" , "author" : "julie"}],
"_id" : "497ce79f1ca9ca6d3efca325"}
如果您使用db.articles.ensureIndex( { comments : 1 } ),它将不会索引 cmets 对象的子字段,而只会索引 cmets 对象本身。
所以下面会使用索引:
> db.posts.find( {comments : { "author" : "julie", "text" : "ok" } } )
因为它是在 cmets 对象上搜索
但以下不会使用索引:
> db.posts.find( { "comments.author" : "julie" } )
那么如何让 mongodb 为第二种情况建立索引?
【问题讨论】:
-
想展示一些关于你如何做事的代码?
-
据我所知,它确实告诉您如何添加此索引:
db.articles.ensureIndex( { tags : 1 } )。
标签: mongodb