aggregation framework - MongoDb get first exists in group stage -


i have data;

[{date :"01-01-2016" , usd: 3 , month : 1, year :2016 }, {date : "02-01-2016" , usd : 2 ,month : 1, year :2016}, {date : "03-01-2016" , usd : 1  ,month : 1, year :2016}, {date : 04-01-2016" , bek : 1 ,month : 1 , year :2016 } ] 

i need first "bek" value average of usd group year-month. when compute aggregation ;

db.serie_data.aggregate([ { $match : { bek : { $exists : true}}   }, { $group: { _id : { year : "$year", month : "$month" }, year : { $first: "$year" }, month : { $first: "$month" }  , usd : { $avg: "$usd } ,bek : { $first: "$bek" } } }, { $project : { year : "$year" , month : "$month" ,  usd : "$usd" , bek :  "$bek"  }}, ]) 

all usd values becoming null, since when bek exists, usd not.

i need this;

[{month:1,year:2016,usd : 2, bek : 1 } ] ,

but : [{month:1,year:2016,usd : null , bek : 1 } ]

i need use $exists statement in $first operator, can first existing column's value.

thanks in advance.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -