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