【发布时间】:2020-03-08 08:16:19
【问题描述】:
我需要从嵌套文档中获取总和值。
数据库文档:
{
"_id": 123,
"products": [
{
"productId": 1,
"charges": [
{
"type": "che",
"amount": 100
}
]
}
]
}
我想得到总和值。
sumValue = products.charges.amount+20; 其中"products.productId" 是1 和"products.charges.type" 是"che"
我尝试了以下查询但没有希望:
db.getCollection('test').aggregate(
[
{"$match":{$and:[{"products.productId": 14117426}, {"products.charges.type":"che"}]},
{ $project: { "_id":0, total: { $add: [ "$products.charges.price", 20 ] } }}
]
)
请帮我解决这个问题。
【问题讨论】:
标签: mongodb aggregation-framework