Javascript Mongo-基于名称的值和的聚合
我试图执行一个聚合函数,根据名称值计算成本和利润值之和。因此,如果多个结果的名称为“假供应商”,我需要成本结果和利润结果的总和 查询语句:Javascript Mongo-基于名称的值和的聚合,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我试图执行一个聚合函数,根据名称值计算成本和利润值之和。因此,如果多个结果的名称为“假供应商”,我需要成本结果和利润结果的总和 查询语句: Pharmacy.aggregate([ { $match: { $and: [{ 'prescription.status': 'Ready for Pickup' }] } }, { $project: { 'insurance.primary.name': 1, 'prescription.financial.cos
Pharmacy.aggregate([
{
$match: {
$and: [{ 'prescription.status': 'Ready for Pickup' }]
}
},
{
$project: {
'insurance.primary.name': 1,
'prescription.financial.cost': 1,
'prescription.financial.margin': 1
}
}
])
结果类似于:
[
{
"_id": "5cab98cd293bd54e94c40461",
"insurance": {
"primary": {
"name": "Fake Provider 1"
}
},
"prescription": [
{
"financial": {
"cost": "2.89",
"margin": "5.60"
}
},
{
"financial": {
"cost": "0.88",
"margin": "1.24"
}
}
]
},
{
"_id": "5cab98d0293bd54e94c40470",
"insurance": {
"primary": {
"name": "Fake Provider 1"
}
},
"prescription": [
{
"financial": {
"cost": "3.22",
"margin": "9.94"
}
},
{
"financial": {
"cost": "2.57",
"margin": "9.29"
}
},
{
"financial": {
"cost": "2.03",
"margin": "10.17"
}
}
]
}
]
[
{
"primaryInsurance": "Fake Provider 1",
"totalFinancialCost": "11.59",
"totalFinancialMargin": "36.24"
},
{
"primaryInsurance": "Fake Provider 2",
"totalFinancialCost": "12.82",
"totalFinancialMargin": "22.16"
}
]
我试图创建一个组语句,但运气不好。此外,成本和利润值当前存储为字符串
$group: {
_id: '$insurance.primary.name',
Financial: {
$push: {
id: '$insurance.primary.name',
name: '$insurance.primary.name',
cost: '$prescription.financial.cost',
margin: '$prescription.financial.margin'
}
}
}
我希望得到与以下类似的结果:
[
{
"_id": "5cab98cd293bd54e94c40461",
"insurance": {
"primary": {
"name": "Fake Provider 1"
}
},
"prescription": [
{
"financial": {
"cost": "2.89",
"margin": "5.60"
}
},
{
"financial": {
"cost": "0.88",
"margin": "1.24"
}
}
]
},
{
"_id": "5cab98d0293bd54e94c40470",
"insurance": {
"primary": {
"name": "Fake Provider 1"
}
},
"prescription": [
{
"financial": {
"cost": "3.22",
"margin": "9.94"
}
},
{
"financial": {
"cost": "2.57",
"margin": "9.29"
}
},
{
"financial": {
"cost": "2.03",
"margin": "10.17"
}
}
]
}
]
[
{
"primaryInsurance": "Fake Provider 1",
"totalFinancialCost": "11.59",
"totalFinancialMargin": "36.24"
},
{
"primaryInsurance": "Fake Provider 2",
"totalFinancialCost": "12.82",
"totalFinancialMargin": "22.16"
}
]
我想我有一个解决方案,可以使用查找和投影返回结果,然后使用javascript映射结果并执行添加。但是,我更喜欢在数据库级别执行此操作。您必须首先展开“处方”字段,然后执行组。尝试此管道:
let pipeline = [
{
$unwind: {
path: '$prescription'
}
},
{
$group: {
_id: '$insurance.primary.name',
totalFinancialCost: {
$sum: { $convert: { input: '$prescription.financial.cost', to: "decimal" } }
},
totalFinancialMargin: {
$sum: { $convert: { input: '$prescription.financial.margin', to: "decimal" } }
}
}
}]
请注意如何将值转换为十进制以执行求和。我收到此错误-MongoError:无法识别的表达式“$convert”。看起来我使用的是v3.6.3。此版本中可能不可用。请尝试使用以下语句:$sum:{$toDecimal:'$prescription.financial.margin'}MongoError:无法识别的表达式“$toDecimal”仅在版本4.0中可用。如果在版本3.6.3中不可能,我将升级,然后重试。转换函数在版本4.0后可用,因此请更新或更改数据类型。