我有这样的架构:
{
"_id" : ObjectId("5c35f04c4e92b8337885d9a6"),
"activity" : {
"a": 10,
"b": 20,
"c": 30,
"d": 40
}
}
现在,我想用一些乘法执行加法和减法,为此,我编写了查询,但是此查询仅对加法有效,而不对减法有效。我的查询是这样:
db.Collection.aggregate([
{ $match: { _id: ObjectId("5c35f04c4e92b8337885d9a6") } },
{ $unwind: "$_id" },
{
$project: {
"activity.score": {
$add: [
{
$multiply: [
"$activity.a",
0.5
]
},
{
$multiply: [
"$activity.b",
0.5
]
}
],
$subtract: [
{
$multiply: [
"$activity.c",
0.2
]
}
]
}
}
}
])
.then(data => {
console.log(data);
})
.catch(error => {
console.log(error);
});
通过得到错误即
'Invalid $project :: caused by :: an expression specification must contain exactly one field, the name of the expression.
如果我删除了减法部分,那么它将正常工作。
任何人都可以建议我在哪里做错了。任何帮助或建议都非常感谢