{
$match: { user: mongodb.ObjectId(userId) }
},
{
$unwind: '$products'
},
{
$project: {
item: '$products.item',
quantity: '$products.quantity'
}
},
{
$lookup: {
from: collection.PRODUCT_COLLECTION,
localField: 'item',
foreignField: '_id',
as: 'product'
}
},
{
$project: {
item: 1, quantity: 1, product: { $arrayElemAt: ['$product', 0] }
}
},
{
$group: {
_id: null,
total: { $sum: { $multiply: [{ $toInt: '$quantity' }, { $toInt: '$product.Price' }] } }
}
}
])
但它说 UnhandledPromiseRejectionWarning: MongoServerError: PlanExecutor error during aggregation :: caused by :: Attempt to convert NaN value to integer type in $convert with no onError value
我建议从数据库中删除特定文档。