mongodb - Trying to populate an array in mongo shell -


im trying populate array describe below results , output give me array 4 null elements

  "result" : [           {                   "maxtemp" : 83,                   "group_id" : "florida"           },           {                   "maxtemp" : 57,                   "group_id" : "new mexico"           },           {                   "maxtemp" : 81,                   "group_id" : "california"           },           {                   "maxtemp" : 57,                   "group_id" : "vermont"           }   ],   "ok" : 1 

the code

db.data.aggregate(             {              $group: {                  _id: "$state"                , maxtemp: { $max :"$temperature"}              }            },            { $project: {                 _id: 0,                 group_id:"$_id",                 maxtemp: "$maxtemp"             }}         ).result.foreach(function(match) {             results.push(db.data.findone(match));         }); 

but output give result

> results [ null, null, null, null ] 

what populate results?

this close valid i'll give "fair" try. problem here changed field names, unless made same not match. hence null responses:

var results = [];  db.data.aggregate([    { "$group": {        "_id": "$state",         "maxtemp": { "$max" :"$temperature"}    }},    { "$project": {        "_id": 0,        "state":"$_id",        "temperature": "$maxtemp"    }} ).result.foreach(function(match) {     results.push(db.data.findone(match)); }); 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -