Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a distinct aggregation of an array field across indexes

Tags:

I'm trying to learn MongoDB and how it'd be useful for analytics for me. I'm simply playing around with the JavaScript console available on their website and have created the following items:

{"title": "Cool", "_id": {"$oid": "503e4dc0cc93742e0d0ccad3"}, "tags": ["twenty", "sixty"]} {"title": "Other", "_id": {"$oid": "503e4e5bcc93742e0d0ccad4"}, "tags": ["ten", "thirty"]} {"title": "Ouch", "_id": {"$oid": "503e4e72cc93742e0d0ccad5"}, "tags": ["twenty", "seventy"]} {"title": "Final", "_id": {"$oid": "503e4e72cc93742e0d0ccad6"}, "tags": ["sixty", "seventy"]} 

What I'd like to do is query so I get a list of unique tags for all of these objects. The result should look something like this:

["ten", "twenty", "thirty", "sixty", "seventy"] 

How do I query for this? I'm trying to distinct() it, but the call always fails without even querying.

like image 730
Naftuli Kay Avatar asked Aug 29 '12 17:08

Naftuli Kay


Video Answer


1 Answers

The code that fails on their website works on an actual MongoDB instance:

> db.posts.insert({title: "Hello", tags: ["one", "five"]}); > db.posts.insert({title: "World", tags: ["one", "three"]}); > db.posts.distinct("tags"); [ "one", "three", "five"] 

Weird.

like image 124
Naftuli Kay Avatar answered Jan 09 '23 00:01

Naftuli Kay