Mongodb 使用聚合和组时如何获取对象的其他字段
我有一个事务模型,其中有事务的描述和状态字段。像下面这样Mongodb 使用聚合和组时如何获取对象的其他字段,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有一个事务模型,其中有事务的描述和状态字段。像下面这样 [ { "_id": "5795e3f4f4a0fb8c1dff20ad", "description": "This is a transcation", "invoice_id": "5795db00bfa9d366194a454d", "amount": 50 }, { "_id": "5795e3faf4a0fb8c1dff20ae", "description": "T
[
{
"_id": "5795e3f4f4a0fb8c1dff20ad",
"description": "This is a transcation",
"invoice_id": "5795db00bfa9d366194a454d",
"amount": 50
},
{
"_id": "5795e3faf4a0fb8c1dff20ae",
"description": "This is a transcation",
"invoice_id": "5795db00bfa9d366194a454d",
"amount": 100
}
]
我使用聚合和分组这些记录来查找匹配对象的totalAmount
。为此,我使用以下查询:
Transaction.aggregate([{
$match: {
invoice: ObjectId("5795db00bfa9d366194a454d")
}
}, {
$group: {
_id: {
_id: "$_id"
},
count: {
$sum: 1
},
totalAmount: {
$sum: "$amount"
},
}
}
])
当我运行这个程序时,我得到以下结果
/* 1 */
{
"_id": {
"_id": ObjectId("5796031e07bad1d21f3af823")
},
"count": 1.0,
"totalAmount": 100
}
/* 2 */
{
"_id": {
"_id": ObjectId("5795e3f4f4a0fb8c1dff20ad")
},
"count": 1.0,
"totalAmount": 50
}
但是我想得到所有匹配的对象,这些对象具有特定的invoice\u id
,以及totalAMount
和其他字段,如description
。应该是这样的
[{
"_id": {
"_id": ObjectId("5795e3f4f4a0fb8c1dff20ad")
},
"description": "description of first object",
"count": 5.0,
"totalAmount": 350
}, {
"_id": {
"_id": ObjectId("5795e3f4f4a0fb8c1dff20ad")
},
"description": "description of second object",
"count": 5.0,
"totalAmount": 350
}]
如何实现此解决方案?经过几次反复澄清(检查您是否真的感兴趣),我编写了下一个查询
db.invoices.aggregate([{
$match: {
"invoice_id": ObjectId("5795db00bfa9d366194a454d")
}
}, {
$group: {
_id: {
_id: "$invoice_id"
},
count: {
$sum: 1
},
info: {
$push: {
id: "$_id",
amount: "$amount",
description: "$description"
}
},
totalAmount: {
$sum: "$amount"
}
}
}, {
$unwind: "$info"
}, {
$project: {
_id: "$info.id",
count: "$count",
invoice_id: "$_id",
totalAmount: "$totalAmount",
amount: "$info.amount",
description: "$info.description"
}
}])
这是怎么回事
totalAmount
,但同时将所有附加信息推送到info
字段中。之后,我们有一个文档,看起来像:
{
"_id" : {
"_id" : "5795db00bfa9d366194a454d"
},
"count" : 2,
"info" : [
{
"id" : ObjectId("5795e3f4f4a0fb8c1dff20ad"),
"amount" : 50,
"description" : "This is a transcation"
},
{
"id" : ObjectId("5796031e07bad1d21f3af823"),
"amount" : 100,
"description" : "FSC evening class"
}
],
"totalAmount" : 150
}
$unwind
,创建两个具有下一个结构的文档
{
"_id" : {
"_id" : "5795db00bfa9d366194a454d"
},
"count" : 2,
"info" : {
"id" : ObjectId("5795e3f4f4a0fb8c1dff20ad"),
"amount" : 50,
"description" : "This is a transcation"
},
"totalAmount" : 150
}
/* 1 */
{
"_id" : ObjectId("5795e3f4f4a0fb8c1dff20ad"),
"count" : 2.0,
"totalAmount" : 150,
"invoice_id" : {
"_id" : ObjectId("5795db00bfa9d366194a454d")
},
"amount" : 50,
"description" : "This is a transcation"
}
/* 2 */
{
"_id" : ObjectId("5796031e07bad1d21f3af823"),
"count" : 2.0,
"totalAmount" : 150,
"invoice_id" : {
"_id" : ObjectId("5795db00bfa9d366194a454d")
},
"amount" : 100,
"description" : "FSC evening class"
}
经过几次反复澄清(检查您是否真的感兴趣),我编写了下一个查询
db.invoices.aggregate([{
$match: {
"invoice_id": ObjectId("5795db00bfa9d366194a454d")
}
}, {
$group: {
_id: {
_id: "$invoice_id"
},
count: {
$sum: 1
},
info: {
$push: {
id: "$_id",
amount: "$amount",
description: "$description"
}
},
totalAmount: {
$sum: "$amount"
}
}
}, {
$unwind: "$info"
}, {
$project: {
_id: "$info.id",
count: "$count",
invoice_id: "$_id",
totalAmount: "$totalAmount",
amount: "$info.amount",
description: "$info.description"
}
}])
这是怎么回事
totalAmount
,但同时将所有附加信息推送到info
字段中。之后,我们有一个文档,看起来像:
{
"_id" : {
"_id" : "5795db00bfa9d366194a454d"
},
"count" : 2,
"info" : [
{
"id" : ObjectId("5795e3f4f4a0fb8c1dff20ad"),
"amount" : 50,
"description" : "This is a transcation"
},
{
"id" : ObjectId("5796031e07bad1d21f3af823"),
"amount" : 100,
"description" : "FSC evening class"
}
],
"totalAmount" : 150
}
$unwind
,创建两个具有下一个结构的文档
{
"_id" : {
"_id" : "5795db00bfa9d366194a454d"
},
"count" : 2,
"info" : {
"id" : ObjectId("5795e3f4f4a0fb8c1dff20ad"),
"amount" : 50,
"description" : "This is a transcation"
},
"totalAmount" : 150
}
/* 1 */
{
"_id" : ObjectId("5795e3f4f4a0fb8c1dff20ad"),
"count" : 2.0,
"totalAmount" : 150,
"invoice_id" : {
"_id" : ObjectId("5795db00bfa9d366194a454d")
},
"amount" : 50,
"description" : "This is a transcation"
}
/* 2 */
{
"_id" : ObjectId("5796031e07bad1d21f3af823"),
"count" : 2.0,
"totalAmount" : 150,
"invoice_id" : {
"_id" : ObjectId("5795db00bfa9d366194a454d")
},
"amount" : 100,
"description" : "FSC evening class"
}
更改代码中group by的顺序:- db.invoices.aggregate([{ $match: { "invoice_id": `ObjectId("5795db00bfa9d366194a454d")` } }, { $group: { _id: { _id: "$invoice_id" }, description: { $first: "$description" } count: { $sum: 1 }, totalAmount: { $sum: "$amount" } } }]) db.invoices.aggregate([{ $match:{ “发票id”:“ObjectId”(“5795db00bfa9d366194a454d”)` } }, { $group:{ _身份证:{ _id:“$invoice\u id” }, 说明:{ $first:“$description” } 计数:{ $sum:1 }, 总额:{ $sum:“$amount” } } }])
更改代码中group by的顺序:- db.invoices.aggregate([{ $match: { "invoice_id": `ObjectId("5795db00bfa9d366194a454d")` } }, { $group: { _id: { _id: "$invoice_id" }, description: { $first: "$description" } count: { $sum: 1 }, totalAmount: { $sum: "$amount" } } }]) db.invoices.aggregate([{ $match:{ “发票id”:“ObjectId”(“5795db00bfa9d366194a454d”)` } }, { $group:{ _身份证:{ _id:“$invoice\u id” }, 说明:{ $first:“$description” } 计数:{ $sum:1 }, 总额:{ $sum:“$amount” } } }])
你怎么能按$_id分组,因为每个文档都是唯一的?@Bhawana你问我?这是作者的代码,他说它在他的案例中如预期的那样工作。我不是他的老板,他不会说这个代码毫无意义。你的回答很有帮助,但请查看我在问题中提到的预期结果。我需要每个匹配对象的TotalAmount。例如,如果有两个匹配对象,且每个对象分别具有金额字段50 100,则每个匹配对象将显示totalAmount=150@MuhammadAteek请阅读我帖子中的更新。你的分组函数对于你的情况是错误的,这就是为什么你会得到不同的结果。您需要按
invoice\u id
分组,而不是按\u id
分组,而不是分别按总金额50和100分组。目前,当我运行您的查询时,我得到了以下形式的结果“/*1/{”{u id:{“{u id”:ObjectId(“5796031e07bad1d21f3af823”),“count”:1.0,“totalAmount”:100,“description”:“FSC夜校”}/2*/{u id:{“{u id”:ObjectId(“5795E3F4FBA08C1DFF20AD”),“count”:1.0,“totalAmount”:50,“描述”:“这是一个转换”}'您如何按$\u id分组,因为它对于每个文档都是唯一的?@Bhawana您问我吗?这是作者的代码,他说它在他的案例中如预期的那样工作。我不是他的老板,他不会说这个代码毫无意义。你的回答很有帮助,但请查看我在问题中提到的预期结果。我需要每个匹配对象的TotalAmount。例如,如果有两个匹配对象,且每个对象分别具有金额字段50 100,则每个匹配对象将显示totalAmount=150@MuhammadAteek请阅读我帖子中的更新。你的分组函数对于你的情况是错误的,这就是为什么你会得到不同的结果。您需要按invoice\u id
分组,而不是按\u id
分组,而不是分别按总金额50和100分组。目前,当我运行您的查询时,我得到了以下形式的结果“/*1/{”{u id:{“{u id”:ObjectId(“5796031e07bad1d21f3af823”),“count”:1.0,“totalAmount”:100,“description”:“FSC夜校”}/2*/{u id:{“{u id”:ObjectId(“5795E3F4FBA08C1DFF20AD”),“count”:1.0,“totalAmount”:50,“描述”:“这是一个转换”}