Question

I am beginning with mongodb and have a collection with documents that look like the following

{
    "type": 1,
    "tags": ["tag1", "tag2", "tag3"]
}
{
    "type": 2,
    "tags": ["tag2", "tag3"]
}
{
    "type": 3,
    "tags": ["tag1", "tag3"]
}
{
    "type": 1,
    "tags": ["tag1", "tag4"]
}

With this, I want a set of all the tags for a particular type. For example, for type 1, I want the set of tag1, tag2, tag3, tag4 (any order).

All I could think of is to get the tags and add them to a set in python, but I wanted to know if there is a way to do it with mongodb's mapreduce or something else. Please advise.

Was it helpful?

Solution

If you just want a (distinct) list of the tags then using distinct will be best. Map/Reduce will be slower and can't use an index for the javascript part.

http://docs.mongodb.org/manual/reference/method/db.collection.distinct/

db.coll.distinct("tags", {type:1}) Will return a set of tags for type=1.

OTHER TIPS

You are right, a Map/Reduce might work for what you are trying to accomplish, but a Set might be faster and less code.

> m =     function() {
...         for (var tag in this.tags) {
...             emit(this.tags[tag], 1);
...         }
...     }

> r =     function(key, values) {
...         return 1;
...     }

> db.tags.mapReduce(m, r).find()
{ "_id" : "tag1", "value" : 1 }
{ "_id" : "tag2", "value" : 1 }
{ "_id" : "tag3", "value" : 1 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top