Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: What's a good way to get a list of all unique tags?

Tags:

mongodb

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.

like image 796
John Galt Avatar asked Jan 04 '10 04:01

John Galt


3 Answers

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

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.

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.

like image 169
kristina Avatar answered Sep 29 '22 12:09

kristina


I know this is an old question, but I had the same issue and could not find a real solution in PHP for it.

So I came up with this:

http://snipplr.com/view/59334/list-of-keys-used-in-mongodb-collection/

like image 39
David Lemcoe Jr. Avatar answered Sep 29 '22 13:09

David Lemcoe Jr.


John, you may find it useful to use Variety, an open source tool for analyzing a collection's schema: https://github.com/jamescropcho/variety

Perhaps you could run Variety every N hours in the background, and query the newly-created varietyResults database to retrieve a listing of unique keys which begin with a given string (i.e. are descendants of a specific parent).

Let me know if you have any questions, or need additional advice.

Good luck!

like image 41
James Cropcho Avatar answered Sep 29 '22 13:09

James Cropcho