Mongodb db version v1.8.1 multiple columns distinct

2,980 views
Skip to first unread message

Jeff

unread,
May 9, 2011, 8:26:21 PM5/9/11
to mongodb-user, jef...@yahoo.com
Hello All,
ok so here is what i am trying to do
i have the following dataset
{ "_id" : ObjectId("4dc86fef6a0aa8513ab5f21c"), "key" : "SAGAR",
"score" : 16, "note" : "test1" }
{ "_id" : ObjectId("4dc86ffd6a0aa8513ab5f21d"), "key" : "SAGAR456",
"score" : 17, "note" : "testjh1" }
{ "_id" : ObjectId("4dc8700b6a0aa8513ab5f21e"), "key" : "SAGAR33",
"score" : 37, "note" : "test2" }
{ "_id" : ObjectId("4dc871686a0aa8513ab5f21f"), "key" : "SAGAR33",
"score" : 37, "note" : "test2" }
{ "_id" : ObjectId("4dc871696a0aa8513ab5f220"), "key" : "SAGAR33",
"score" : 37, "note" : "test2" }
{ "_id" : ObjectId("4dc8716c6a0aa8513ab5f221"), "key" : "SAGAR456",
"score" : 17, "note" : "testjh1" }

and i would like find all the distinct key and score combo i have
searched the web for a while now and here is what i found
db.test.distinct({"key":true,"score":true})

my expected results would be :
[{"key" : "SAGAR", "score" : 16},
{ "key" : "SAGAR456", "score" : 17},
{"key" : "SAGAR33", "score" : 37}]
or something of the like

Thanks for the help

Nat

unread,
May 9, 2011, 10:06:21 PM5/9/11
to mongodb-user
Distinct supports only one field at a time. To work around it, you can
either use group or mapReduce command
https://jira.mongodb.org/browse/SERVER-2293

Chris Westin

unread,
May 10, 2011, 1:04:52 PM5/10/11
to mongodb-user
Here's an example of how to do this using .group(), demonstrated via a
mongo shell test script:

db.free263.drop();

db.free263.save({ "_id" : ObjectId("4dc86fef6a0aa8513ab5f21c"),
"key" : "SAGAR",
"score" : 16, "note" : "test1" } );

db.free263.save({ "_id" : ObjectId("4dc86ffd6a0aa8513ab5f21d"),
"key" : "SAGAR456",
"score" : 17, "note" : "testjh1" } );

db.free263.save({ "_id" : ObjectId("4dc8700b6a0aa8513ab5f21e"),
"key" : "SAGAR33",
"score" : 37, "note" : "test2" } );

db.free263.save({ "_id" : ObjectId("4dc871686a0aa8513ab5f21f"),
"key" : "SAGAR33",
"score" : 37, "note" : "test2" } );

db.free263.save({ "_id" : ObjectId("4dc871696a0aa8513ab5f220"),
"key" : "SAGAR33",
"score" : 37, "note" : "test2" } );

db.free263.save({ "_id" : ObjectId("4dc8716c6a0aa8513ab5f221"),
"key" : "SAGAR456",
"score" : 17, "note" : "testjh1" } );

// see http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct
// .distinct() can only take a single key, so you can only do either
d1 = db.free263.distinct("key");
// or
d2 = db.free263.distinct("score");

// .group() does not have that limitiation:
// http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group
// for this simple example, the "reduction" is to just record the
values
g1 = db.free263.group({
key: {key:1, score:1},
reduce: function(obj, prev) { if (!obj.hasOwnProperty("key")) {
prev.key = obj.key;
prev.score = obj.score;
}},
initial: { }
});

----
Beware of the return document size limitation (see the doc link above)
if you do use .group(); if that's an issue, you may have to use full-
blown map-reduce, with functions similar to those above.

Chris

Sergei Tulentsev

unread,
May 10, 2011, 2:44:21 PM5/10/11
to mongod...@googlegroups.com
Doc size constraint also applies to the 'unique' command.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.




--
Best regards,
Sergei Tulentsev
Reply all
Reply to author
Forward
0 new messages