我开始使用mongodb并拥有一个包含如下文档的集合
{
"type": 1,
"tags": ["tag1", "tag2", "tag3"]
}
{
"type": 2,
"tags": ["tag2", "tag3"]
}
{
"type": 3,
"tags": ["tag1", "tag3"]
}
{
"type": 1,
"tags": ["tag1", "tag4"]
}
Run Code Online (Sandbox Code Playgroud)
有了这个,我想要一组特定类型的所有标签.例如,对于类型1,我想要tag1, tag2, tag3, tag4
(任何顺序)的集合.
我能想到的只是获取标签并将它们添加到set
python中,但我想知道是否有办法用mongodb的mapreduce或其他东西来做.请指教.
Sco*_*dez 42
如果你只想要一个(不同的)标签列表,那么使用distinct是最好的.Map/Reduce会慢一点,不能使用javascript部分的索引.
http://docs.mongodb.org/manual/reference/method/db.collection.distinct/
db.coll.distinct("tags", {type:1})
将为type = 1返回一组标记.