本文介绍了MongoDB如何索引数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在MongoDB中,如果我要在字段color中存储一个数组(比如
,是否索引 [red,blue]
) red
和blue
所以我可以查询red,或者make {red,blue}
复合索引?
In MongoDB, if I were to store an array (say ["red", "blue"]
) in a field "color"
, does it index "red"
and "blue"
so I could query for "red"
, for example, or does in make {"red", "blue"}
a composite index?
推荐答案
当涉及到索引数组时,MongoDB会索引数组的每个值,以便您可以查询单个项目,例如red 。例如:
When it comes to indexing arrays, MongoDB indexes each value of the array so you can query for individual items, such as "red." For example:
> db.col1.save({'colors': ['red','blue']})
> db.col1.ensureIndex({'colors':1})
> db.col1.find({'colors': 'red'})
{ "_id" : ObjectId("4ccc78f97cf9bdc2a2e54ee9"), "colors" : [ "red", "blue" ] }
> db.col1.find({'colors': 'blue'})
{ "_id" : ObjectId("4ccc78f97cf9bdc2a2e54ee9"), "colors" : [ "red", "blue" ] }
有关更多信息,请查看MongoDB关于Multikeys的文档:
For more information, check out MongoDB's documentation on Multikeys: http://www.mongodb.org/display/DOCS/Multikeys
这篇关于MongoDB如何索引数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!