MongoDB - 索引内部对象列表 [英] MongoDB - index an internal list of objects
问题描述
假设MongoDB中有以下数据结构:
Assume the following data structure in MongoDB:
{
"_id" : "Bob Blocker",
"ratings" : {
"771206753" : 1
},
"prevalence" : 1
}
如何使用 ensureIndex(索引
?我想建立一个索引,将文档评级
的内部结构的键进行索引? ) _id
与评级对象中的任何和所有键(在本例中为771206753)相关联。
How could one index the keys of the internal structure of ratings
using ensureIndex()
? I would want to build an index that associated the document _id
with any and all of the keys (in this example, "771206753") in the ratings object.
用评级
键入索引会不够?那不会记录评级对象的价值部分吗?如果特定键需要查找,我是否应该移动模式?
Would keying the index with ratings
suffice? Wouldn't that also record the value portion of the ratings object? Should I shift my schema if lookup is intended by that particular key?
推荐答案
您无法索引动态键和索引 rating
会将整个对象编入索引,因此您也不想这样做。
You can't index dynamic keys and indexing ratings
would index the whole object as a blob so you don't want to do that either.
它可能更好用将您的架构重新修改为:
It may work better to rework your schema to:
{
"_id" : "Bob Blocker",
"ratings" : [
{id: "771206753", value: 1}
],
"prevalence" : 1
}
然后指数'ratings.id'
。
这篇关于MongoDB - 索引内部对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!