MongoDB,数组索引 [英] MongoDB, array of arrays index

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

问题描述

我可以在子数组上建立索引吗?

Can I have an index on subarrays?

示例文档:

{ 
    'who': [['en', 'Thomas'], ['es', 'Alfonzo'], ['de', 'Helmut']],
    'otherField': 123
}

查询:

{'who': 
    { '$elemMatch': { '1': 'Helmut'} } 
}

我需要为此类查询创建索引.谢谢.

I need an index for this type of queries. Thanks.

推荐答案

使用这样的数组并不是最好的结构,因为你在数据上强加了几个相当的条件.

Using arrays like this isn't the best structure, because you're forcing several rather conditions on your data.

像这样

...
"who": [{
  "language": "en", "text": "Thomas'
}, ... ]
...

(删除以前的建议)

然后,正如 Jim Dagg 指出的那样,您可以在 who.text 上创建索引.

Then, as Jim Dagg pointed out, you could create an index on who.text.

这篇关于MongoDB,数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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