How are MongoDB index arrays?

In MongoDB, if I were to store an array (say ["red", "blue"] ) in the "color" field, it indexes "red" and "blue" so that I can query for "red" , for example, or in make {"red", "blue"} composite index?

+53
indexing mongodb
30 Oct '10 at 14:30
source share
1 answer

When it comes to indexing arrays, MongoDB indexes each value in the array, so you can query for individual elements, 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" ] } 

For more information, check out MongoDB documentation on Multikeys: http://www.mongodb.org/display/DOCS/Multikeys

+74
Oct 30 '10 at 20:01
source share



All Articles