top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Aggregation and Find Operator in MongoDB?

+2 votes
281 views

I have an use case where I have find all the matching records and apply the aggregation like count, sum, group by etc .
Can I use such scenarios where I have to use find and aggregate function together or should I find then get the cursor object and have to perform aggregation operation separately

posted Mar 12, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

Here is the script example you can edit it and use in as per your requirement,
MongoDB Script:

db.collectionname.group({
    "key": {
        "UserName": true
    },
    "initial": {
        "sumscore": 0,
        "sumforaverageaveragescore": 0,
        "countforaverageaveragescore": 0,
        "countstar": 0
    },
    "reduce": function(obj, prev) {
        prev.sumscore = prev.sumscore + obj.score - 0;
        prev.sumforaverageaveragescore += obj.score;
        prev.countforaverageaveragescore++;
        prev.minimumvaluescore = isNaN(prev.minimumvaluescore) ? obj.score : Math.min(prev.minimumvaluescore, obj.score);
        prev.maximumvaluescore = isNaN(prev.maximumvaluescore) ? obj.score : Math.max(prev.maximumvaluescore, obj.score);
        if (true != null) if (true instanceof Array) prev.countstar += true.length;
        else prev.countstar++;
    },
    "finalize": function(prev) {
        prev.averagescore = prev.sumforaverageaveragescore / prev.countforaverageaveragescore;
        delete prev.sumforaverageaveragescore;
        delete prev.countforaverageaveragescore;
    },
    "cond": {
        "score": {
            "$gt": 0
        },
        "UserName": {
            "$in": ["shivam", "anonymous"]
        }
    }
});

its SQL script is:

SELECT UserName, SUM(score), AVG(score), MIN(score), MAX(score), COUNT(*) 
FROM collectionname
WHERE score > 0 AND UserName IN('shivam','anonymous') 
GROUP BY UserName;

Let me know if it fulfills your requirement.Thanks.

answer Mar 14, 2016 by Shivam Kumar Pandey
Similar Questions
0 votes

I am new to mongodb. I am trying to do some aggregation operations like sum, avg, min.. on a collection. And I found that I can do it either using aggregation framework or cursor.forEach(). Which one to use? It will be better if someone explains how both works internally and give me some suggestions.

Thank you in advance

0 votes

I have an aggregation query given below which I need to pass in as a json string to java app to execute it. Is it possible to do that -

{$unwind:$results},{$match:{result.studentid:11}}

I tried using BasicDBObject.parse but it is returning me the unwinded or flattened results and the result is not segregating on the basis of match.

Is there any other way to do it.

+1 vote

I am having collection with 127706 document. In aggregation pipeline i having 2 group stages. It is giving me result in 1.5 sec.

To optimize it to more I have created index on the fields which I am using in match stages with no success. Is their any to optimize aggregation performance in more way?

I m using mongodb 3.2.1?

+2 votes

Please tell me how can we resolve this issue, we are using aggregation and size of docs become greater than 16MB. We also tried "ALLOWDISKUSE = TRUE" but still its throwing same error.

We are using php to fetch data greater than 16MB. Let me know if you needed more info.

...