gpt4 book ai didi

mongodb - 在 mongodb 中创建具有非聚合字段的 View 而不将其放入 _id 字段

转载 作者:行者123 更新时间:2023-12-04 08:13:16 24 4
gpt4 key购买 nike

我有一个包含下面给出的数据的集合

{
"_id" : ObjectId("5fc8a58257f6526c6a520725"),
"job_id" : ObjectId("5fc8a5822aed5820240c2893"),
"access_id" : ObjectId("5eda0b2495b51c1d746db8b2"),
"campaign_num" : "488707373",
"partner_id" : ObjectId("5a94e02bb0445b1cc742d735"),
"parent_partner" : ObjectId("5a8d70c4b0445b6a883fd395"),
"status" : "A",
"route" : "SR",
"otp" : true,
"trans" : false,
"job_payload" : {
"senderid" : "36",
"msisdn" : "9671660999",
"script" : "Your verification code is : 166297",
"pingbackurl" : "https://xyztesting.com/sms/v1/notify/pr/index.php",
"templateid" : "769",
"smstype" : "2"
},
"track" : {
"added" : {
"date" : ISODate("2020-12-03T08:44:50.000Z")
},
"routed" : ISODate("2020-12-03T08:44:50.000Z")
},
"message_parts" : 1,
"provider_status" : {
"job_id" : "",
"count" : {
"total" : 1,
"sent" : 0,
"delivered" : 0,
"failed" : 0
},
"delivery" : []
}
}
我想计算所有总数,发送,交付,失败,其中 Campaign_num 和 date 相同。即在同一日期使用相同的campaign_num。
我使用以下查询创建了 View
var pipeline = [
{
$group: {
_id: {
campaign: "$campaign_num",
date: { $dateToString: { format: "%Y-%d-%m", date: "$track.added.date" } }
},
total: { $sum: "$provider_status.count.total" },
sent: { $sum: "$provider_status.count.sent" },
delivered: { $sum: "$provider_status.count.delivered" },
failed: { $sum: "$provider_status.count.failed" }
}
}
]
db.createView("sms_daily_test","col_outbox",pipeline)
我得到如下所示的输出
 "_id" : {
"campaign" : "488707373",
"date" : "2021-19-01"
},
"total" : 3,
"sent" : 3,
"delivered" : 0,
"failed" : 0
}
现在的主要问题是我想包括 partner_id 和 access_id 字段,但不在 _id 中。即我希望输出是这样的
"_id" : {
"campaign" : "488707373",
"date" : "2021-19-01"
},
"total" : 3,
"sent" : 3,
"delivered" : 0,
"failed" : 0,
"partner_id":ObjectId("5a94e02bb0445b1cc742d735"),
"access_id":ObjectId("5a94e02bb0445b1cc742d635")
}
我正在使用以下代码
var pipeline = [
{
$group: {
_id: {
campaign: "$campaign_num",
date: { $dateToString: { format: "%Y-%d-%m", date: "$track.added.date" } }
},
partner_id: "$partner_id",
access_id: "$access_id",
total: { $sum: "$provider_status.count.total" },
sent: { $sum: "$provider_status.count.sent" },
delivered: { $sum: "$provider_status.count.delivered" },
failed: { $sum: "$provider_status.count.failed" }
}
}
]
db.createView("sms_daily_testing2","col_outbox",pipeline)
当我运行它时,我收到以下错误消息: The field 'partner_id' must be an accumulator object .
有关此字段的信息是特定事件将只有一个 partner_id和一个 access_id .虽然 partner_id可以有多个广告系列和一个 access_id也可以有多个广告系列。
我需要按我已经完成的事件和日期字段对数据进行分组。我只需要知道如何添加这两个字段而不将其放在 _id 里面 field 。

最佳答案

您已经写到给定的事件和日期将具有唯一的 partner_id 和 access_id。我们必须在 _id 字段之外使用累加器对象。由于我们只有一个合作伙伴和 access_id,我们可以使用 $first 或 $last ,如下所示:

 var pipeline=[{$group:{_id:{campaign:"$campaign_num",date:{$dateToString:{format:"%Y-%m-%d",date:"$track.added.date"}}},
partner_id:{$first:"$partner_id"},
access_id:{$first:"$access_id"},
total: {$sum: "$provider_status.count.total"},
sent:{$sum:"$provider_status.count.sent"},
delivered:{$sum:"$provider_status.count.delivered"},
failed:{$sum:"$provider_status.count.failed"}}
}]

关于mongodb - 在 mongodb 中创建具有非聚合字段的 View 而不将其放入 _id 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65840159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com