获取mongodb中所有唯一标签的列表 [英] Get a list of all unique tags in mongodb

查看:55
本文介绍了获取mongodb中所有唯一标签的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从mongodb开始,有一个包含如下文档的集合

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"]
}

有了这个,我想要一个特定类型的所有标签的一个 set .例如,对于类型1,我想要一组tag1, tag2, tag3, 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).

我所能想到的就是获取标记并将它们添加到python中的set中,但是我想知道是否有一种方法可以使用mongodb的mapreduce或其他方法.请告知.

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.

推荐答案

如果您只想要标签的(不同的)列表,则最好使用distinct. Map/Reduce会变慢,并且无法在Javascript部分使用索引.

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})将返回一组类型为1的标签.

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

这篇关于获取mongodb中所有唯一标签的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆