sum of two fields without grouping

2,273 views
Skip to first unread message

Junaid Malik

unread,
Sep 14, 2011, 10:09:15 AM9/14/11
to mongodb-user
I want to sum two fields of a collection without grouping anything as
data is already grouped. I just need to give date criteria.

SQL query : SELECT sum(sell + purchase) AS amount FROM
transcations where date > '2/2/2011' and date < '3/2/2011'

Can anyone guide me how can i do this in mongodb using find?

Thanks
Junaid

Nat

unread,
Sep 14, 2011, 10:20:28 AM9/14/11
to mongod...@googlegroups.com
Mongodb doesn't do that. You need to do it manually on the client side.

Tomasz Kułakowski

unread,
Sep 14, 2011, 10:46:26 AM9/14/11
to mongod...@googlegroups.com
using PHP? $collection->find('date'=>array('$gte'=>'2/2/2011','$lte'=>'3/2/2011'))
using mongo shell db.collection.find({date:{$gte:'2/2/2011',$lte:'3/2/2011'}})


2011/9/14 Junaid Malik <junaid...@pikessoft.com>

--
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.


Junaid Malik

unread,
Sep 14, 2011, 1:35:08 PM9/14/11
to mongodb-user
Thanks Tomasz. I'm using ruby on rails. I will try group clause to
achieve this.

On Sep 14, 7:46 pm, Tomasz Kułakowski <tomekkulakow...@gmail.com>
wrote:
> using PHP?
> $collection->find('date'=>array('$gte'=>'2/2/2011','$lte'=>'3/2/2011'))
> using mongo shell
> db.collection.find({date:{$gte:'2/2/2011',$lte:'3/2/2011'}})
>
> 2011/9/14 Junaid Malik <junaid.ma...@pikessoft.com>

Junaid Malik

unread,
Sep 14, 2011, 1:36:49 PM9/14/11
to mongodb-user
Thanks Nat.

Junaid Malik

unread,
Sep 14, 2011, 1:34:10 PM9/14/11
to mongodb-user
Thanks Nat for help.

On Sep 14, 7:20 pm, Nat <nat.lu...@gmail.com> wrote:

Adam C

unread,
Dec 4, 2012, 9:43:17 AM12/4/12
to mongod...@googlegroups.com
As long as you can return the results in memory (same as the inline MR), then yes, you could just use $group and $sum to achieve this:


Adam


On Tuesday, December 4, 2012 4:41:35 AM UTC, Adam Gotterer wrote:
Curious if you could replace the map reduce version with something from the aggregation framework?

On Thursday, August 9, 2012 9:37:33 AM UTC-4, ko wrote:

The post it's a little old, but maybe this will help other persons:
you can with map/reduce using finalize:
db.runCommand({
        "mapreduce": "coll",
        "map": function() {
                emit({id:this.id},{
                        sell: this.sell,
                        purchase: this.purchase
                     });  
        },
        "reduce": function(key,vals) {
                return 1;
        },
        "out": {
                inline : 1
        },
        "finalize": function(key, vals) {
                return { amount: vals.sell + vals.purchase};
        }
});

Reply all
Reply to author
Forward
0 new messages