我想找到与团队和年份相等的所有文件。 created_at字段包含日期。日期聚合运算符$ month可以在查询的第二部分找到,但是eq语法令人困惑。
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”:“错误的查询:BadValue:未知的顶级操作符:$ eq”,
示例文档:
{
"_id" : ObjectId("5824dc9134ddc60d2e5d0a66"),
"team_id" : "5824dc9134ddc60d2e5d0a64",
"salary" : 60,
"created_at" : ISODate("2016-11-10T20:46:07.045Z"),
"__v" : 0
}
答案 0 :(得分:3)
你可以先预测一年:
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 } } }