MongoDB - 索引内部对象列表 [英] MongoDB - index an internal list of objects

查看:116
本文介绍了MongoDB - 索引内部对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆