Aggregate match query with date extraction in MongoDB -


i find documents equal team , year. created_at field holds date. date aggregation operator $month works find in second part of query eq syntax confusing.

db.getcollection('datas').aggregate([     { $match : { team_id: '5824a4623bfd05018197792d', $eq: [{ $year: "$created_at" },"2016"] } },     { $group: { _id: { $month: "$created_at" }, avg: { $avg: "$salary" }, count: { $sum: 1 } } } ]); 

"errmsg" : "bad query: badvalue: unknown top level operator: $eq",

sample document:

{     "_id" : objectid("5824dc9134ddc60d2e5d0a66"),     "team_id" : "5824dc9134ddc60d2e5d0a64",     "salary" : 60,     "created_at" : isodate("2016-11-10t20:46:07.045z"),     "__v" : 0 } 

you can projecting year first:

db.getcollection("datas").aggregate([     { $match: { team_id: "5824a4623bfd05018197792d" } },     { $project: { salary: "$salary", created_at: "$created_at", year: { $year: "$created_at" } } },     { $match: { year: 2016 } },     { $group: { _id: { $month: "$created_at" }, avg: { $avg: "$salary" }, count: { $sum: 1 } } } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -