Mongodb provides an interesting "Multi-key" feature that automatically indexes the array values of objects. Tags are a good example. Suppose you have an article that contains many classification tags:
$ dbshell
> db.articles.save( { name: "Warm Weather", author: "Steve",
tags: ['weather', 'hot', 'record', 'april'] } )
> db.articles.find()
{"name" : "Warm Weather" , "author" : "Steve" ,
"tags" : ["weather","hot","record","april"] ,
"_id" : "497ce4051ca9ca6d3efca323"}
We can easily query the values in the tags array:
> db.articles.find( { tags: 'april' } )
{"name" : "Warm Weather" , "author" : "Steve" ,
"tags" : ["weather","hot","record","april"] ,
"_id" : "497ce4051ca9ca6d3efca323"}
Furthermore, we can index the tag array. The index created on the array will index each element of the array:
> db.articles.ensureIndex( { tags : 1 } )
true
> db.articles.find( { tags: 'april' } )
{"name" : "Warm Weather" , "author" : "Steve" ,
"tags" : ["weather","hot","record","april"] ,
"_id" : "497ce4051ca9ca6d3efca323"}
> db.articles.find( { tags: 'april' } ).explain()
{"cursor" : "BtreeCursor tags_1" , "startKey" : {"tags" : "april"} ,
"endKey" : {"tags" : "april"} , "nscanned" : 1 , "n" : 1 , "millis" : 0 }
Add and delete keywords
You can use $ addToSet to add a new key to the array and then use $ pull to remove this keyword.
> db.articles.update({name: "Warm Weather"},{$addToSet:{tags:"northeast"}});
> db.articles.find();
...
> db.articles.update({name: "Warm Weather"},{$pull:{tags:"northeast"}});
Embedded objects in the array
The same technique can also be used to find fields of objects embedded in the array:
> // find posts where julie commented
> db.posts.find( { "comments.author" : "julie" } )
{"title" : "How the west was won",
"comments" : [{"text" : "great!" , "author" : "sam"},
{"text" : "ok" , "author" : "julie"}],
"_id" : "497ce79f1ca9ca6d3efca325"}
Queries all values in a given set
You can use the $ all option to specify values that match the values in all given sets. For example:
> db.articles.find( { tags: { $all: [ 'april', 'record' ] } } )
{"name" : "Warm Weather" , "author" : "Steve" ,
"tags" : ["weather","hot","record","april"] ,
"_id" : "497ce4051ca9ca6d3efca323"}
> db.articles.find( { tags: { $all: [ 'april', 'june' ] } } )
> // no matches
Warning on parallel arrays
A composite index can contain at most one array keyword. Therefore, if we have an index {a: 1, B: 1}, the following documents are legal:
{a: [1, 2], b: 1}
{a: 1, b: [1, 2]}
The following document fails to be inserted and prompts "you cannot create an index on parallel arrays ":
{a: [1, 2], b: [1, 2]}
This problem occurs because every value in the Cartesian product of the composite key is indexed. (If parallel arrays are supported, the Cartesian product will be large, resulting in a very large index ), this may soon be out of control.
Replace a large number of indexes with multiple keys
One way to query data that contains a lot of optional data is to use a multi-key index, where keys are all objects. For example:
> x = {
... _id : "abc",
... cost : 33,
... attribs : [
... { color : 'red' },
... { shape : 'rect' },
... { color : 'blue' },
... { avail : true } ]
... };
> db.foo.insert(x);
> db.foo.ensureIndex({attribs:1});
> db.foo.find( { attribs : {color:'blue'} } ); // uses index
> db.foo.find( { attribs : {avail:false} } ); // uses index
In addition to containing unlimited attribute types, we can also dynamically add types.
This is useful in property query. The above mode is not very useful for sorting or other types of queries.