Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb - how to find and then aggregate

Tags:

mongodb

I have collection that contains documents with below schema. I want to filter/find all documents that contain the gender female and aggregate the sum of brainscore. I tried the below statement and it shows a invalid pipeline error.

db['!all'].aggregate({ $and: [ {'GENDER' :  'F'} , {'DOB' : { $gte : 19400801, $lte : 20131231 }} ]  }, { $group : { _id : "$GENDER", totalscore : { $sum : "$BRAINSCORE" } } } ) 

Schema:

{     "_id" : ObjectId("53f63fc8f2b643f6ebb8a1a9"),     "DOB" : 19690112,     "GENDER" : "F",     "BRAINSCORE" : 65 }, {     "_id" : ObjectId("53f63fc8f2b643f6ebb8a1a2"),     "DOB" : 19950116,     "GENDER" : "F",     "BRAINSCORE" : 44 }, {     "_id" : ObjectId("53f63fc8f2b643f6ebb8a902"),     "DOB" : 19430216,     "GENDER" : "M",     "BRAINSCORE" : 71 } 
like image 356
LAX_DEV Avatar asked Aug 21 '14 21:08

LAX_DEV


People also ask

Can you use Find and aggregate in MongoDB?

To create an aggregation pipeline, you can use can use MongoDB's aggregate() method. This method uses a syntax that is fairly similar to the find() method used to query data in a collection, but aggregate() accepts one or more stage names as arguments. This step focuses on how to use the $match aggregation stage.

What are the differences between using aggregate () and find () in MongoDB?

With aggregate + $match, you get a big monolithic BSON containing all matching documents. With find, you get a cursor to all matching documents. Then you can get each document one by one.

How do you do aggregate queries in MongoDB?

Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. In SQL count(*) and with group by is an equivalent of MongoDB aggregation.

Is aggregate faster than find?

Without seeing your data and your query it is difficult to answer why aggregate+sort is faster than find+sort. A well indexed(Indexing that suits your query) data will always yield faster results on your find query.


2 Answers

You have to use $match:

db['!all'].aggregate([   {$match:     {'GENDER': 'F',      'DOB':       { $gte: 19400801,         $lte: 20131231 } } },   {$group:      {_id: "$GENDER",      totalscore:{ $sum: "$BRAINSCORE"}}} ]) 

Outputs:

{ "_id" : "F", "totalscore" : 109 } 
like image 111
Enrique Fueyo Avatar answered Sep 29 '22 04:09

Enrique Fueyo


Sample working query :

db.getCollection('NOTIF_EVENT_RESULT').aggregate([ {$match:     {'userId': {'$in' : ['user-900', 'user-1546']},     'criteria.operator': 'greater than', 'criteria.thresold' : '90', 'category' : 'capacity'} }, {"$group" :  {_id : {userId:"$userId"}, "count" : { "$sum" : 1} } } ]) 
like image 34
Binita Bharati Avatar answered Sep 29 '22 04:09

Binita Bharati