user1429322
user1429322

Reputation: 1286

Top n elements in mongodb collection

I have a mongo db collection which has entried like this

{
      _id:
      sender:
      receiver:
}

Is there anyway of getting top N in sender and receiver in the collection with/without using mapreduce? I am using mongodb ruby driver.

Upvotes: 1

Views: 510

Answers (2)

Jenna
Jenna

Reputation: 2396

There is a limit parameter associated with map reduce, but the documentation says that you can't use it with sharded clusters. Can you output the results of map reduce to a new collection and do a .find().limit(n) query on the collection? Or are you attempting to avoid a long running aggregation command, which is why you're interested in limiting the output in the first place? Or did I misunderstand your question?

input:

{ "_id" : 1, "sender" : "Jenna"}
{ "_id" : 2, "sender" : "Jenna"}
{ "_id" : 3, "sender" : "George"}
{ "_id" : 4, "sender" : "George"}
{ "_id" : 5, "sender" : "Amy"}

MR functions

map = function () {
    emit(this.sender, 1);
}

reduce = function (key, values) {
    return 1;
}

output:

"results" : [
        {
            "_id" : "Amy",
            "value" : 1
        },
        {
            "_id" : "George",
            "value" : 1
        },
        {
            "_id" : "Jenna",
            "value" : 1
        }
    ]

Upvotes: 1

Jenna
Jenna

Reputation: 2396

If possible, I would check out the new aggregation framework available in MongoDB 2.2 (the next stable release). Is this what you're trying to accomplish?

input:

{_id:1, name:"Jenna"}
{_id:2, name:"Jenna"}
{_id:3, name:"Tom"}
{_id:4, name:"Tom"}
{_id:5, name:"George"}
{_id:6, name:"George"}

command:

> db.unicorn.aggregate({$group: {_id: "$name"}},{$limit:2})

result:

{
    "result" : [
        {
            "_id" : "George"
        },
        {
            "_id" : "Tom"
        }
    ],
    "ok" : 1
}

www.mongodb.org/display/DOCS/Aggregation+Framework

Upvotes: 1

Related Questions