node.js - Aggregate in mongoDB -


i did @ lot of code example never found out want. loop prefer use mongo engine result want it. more crazy begin spend lot of time on , looks basic ...

i'm running nodejs express , mongodb.

basically have collection information :

{   "aqi": 149,   "area": "al",   "position_name": "montgomery",   "station_code": "1793a", }, {   "aqi": 162,   "area": "al",   "position_name": "huntsville",   "station_code": "1790a", }, {   "aqi": 73,   "area": "tx",   "position_name": "dallas",   "station_code": "1792a", } 

i result dataset like

{   "_id": "al",   "positions": [     {       "position_name": "montgomery",       "station_code": "1793a"     },     {       "position_name": "huntsville"       "station_code": "1790a"     }   ] }, {   "_id": "tx",   "positions": [     {       "position_name": "dallas",       "station_code": "1793a"     }   ] } 

so far think did check on stackoverflow , try several solutions, meanwhile if i'm wrong , questions similar problem exist, please kindly let me know , apologize in advance.

my current code :

db.collection('cities', function(err, collection) {   collection.aggregate([{$group : {_id : "$area"}}], function(err, items) {     res.send(items);   }); }); 

i thinking mix match (area) , group no success until now.

thank help.

edit :

i did forget group operator this. here new code :

db.collection('cities', function(err, collection) {       collection.aggregate([{$group : {_id: "$area", positions: {$push : "$position_name"}}}], function(err, items) {     res.send(items);   }); }); 

i think first need come terms being aggregation pipeline , important thing thinking piping results 1 shell command another, doing in aggregation.

so got started need more steps. without spoon feeding i'll @ $push operator , should point in right direction.

also try spend time going through operators , tutorial examples given.

http://docs.mongodb.org/manual/reference/operator/aggregation/

http://docs.mongodb.org/manual/tutorial/aggregation-zip-code-data-set/


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -