MongoDB:获取所有唯一标签列表的好方法是什么? [英] MongoDB: What's a good way to get a list of all unique tags?
问题描述
跟踪成千上万个大型项目的文档集合的唯一标签的最佳方法是什么?进行标记的通常方法似乎是索引多键.不过,我经常需要获取所有唯一键.我也无法访问mongodb的新"distinct"命令,因为我的驱动程序erlmongo似乎尚未实现.
What's the best way to keep track of unique tags for a collection of documents millions of items large? The normal way of doing tagging seems to be indexing multikeys. I will frequently need to get all the unique keys, though. I don't have access to mongodb's new "distinct" command, either, since my driver, erlmongo, doesn't seem to implement it, yet.
推荐答案
即使您的驱动程序没有实现distinct,也可以自己实现.用JavaScript(对不起,我不知道Erlang,但是它应该直接翻译)可以说:
Even if your driver doesn't implement distinct, you can implement it yourself. In JavaScript (sorry, I don't know Erlang, but it should translate pretty directly) can say:
result = db.$cmd.findOne({"distinct" : "collection_name", "key" : "tags"})
也就是说,您在使用任何数据库的"$ cmd"集合上执行一个findOne.向其传递集合名称和您要在其上运行的键.
So, that is: you do a findOne on the "$cmd" collection of whatever database you're using. Pass it the collection name and the key you want to run distinct on.
如果您需要驱动程序不提供帮助的命令,可以查看 http://www.mongodb.org/display/DOCS/List+of+Database+Commands 以获得完整的数据库命令列表.
If you ever need a command your driver doesn't provide a helper for, you can look at http://www.mongodb.org/display/DOCS/List+of+Database+Commands for a somewhat complete list of database commands.
这篇关于MongoDB:获取所有唯一标签列表的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!