【发布时间】:2021-07-27 19:40:57
【问题描述】:
我正在尝试使用用户 ID 和服务状态来总结用户已完成的服务总量。我的收藏是这样的:
[
{
_id: '5543333',
title: 'service 1',
description: 'des 1',
status: 'completed',
amount: 3000,
user_id: '1',
},
{
_id: '5543563',
title: 'service 2',
description: 'des 2',
status: 'in progress',
amount: 5000,
user_id: '1',
},
{
_id: '5542933',
title: 'service 3',
description: 'des 3',
status: 'completed',
amount: 4000,
user_id: '1',
},
];
预期结果:[{total: 7000}]
我尝试过的:
db.services.aggregate([
{
$group: {
_id: '',
price: {
$sum: {
$cond: [
{
$and: [
{ $eq: ['$status', 'completed'] },
{ $eq: ['$user_id', user.id] },
],
},
'$price',
0,
],
},
},
},
},
{
$project: {
_id: 0,
total: '$price',
},
},
]);
我得到的结果:[{total: 0}]
我的观察:它适用于单个条件而不是多个条件。
【问题讨论】:
标签: javascript node.js mongodb nosql aggregation-framework