gpt4 book ai didi

mongodb - 无法在聚合管道上从 BSON 类型字符串转换为 Date

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

我使用的是 MongoDB 3.4.9,我想要月度报告 w.r.t.客户信息,这里是带有嵌套项目和收到错误的示例示例 mongodb 记录是:

can't convert from BSON type string to Date

    {
"_id" : ObjectId("59da6a331c7a9ac0b6674fe8"),
"date" : ISODate("2017-10-08T18:10:59.899Z"),
"items" : [
{
"quantity" : 1,
"price" : 47.11,
"desc" : "Item #1"
},
{
"quantity" : 2,
"price" : 42.0,
"desc" : "Item #2"
}
],
"custInfo" : "Tobias Trelle, gold customer"
}

{
"_id" : ObjectId("59da6a511c7a9ac0b6674fed"),
"date" : ISODate("2017-10-08T18:11:28.961Z"),
"items" : [
{
"quantity" : 1,
"price" : 47.11,
"desc" : "Item #1"
},
{
"quantity" : 2,
"price" : 42.0,
"desc" : "Item #2"
}
],
"custInfo" : "Tobias Trelle, gold customer"
}

{
"_id" : ObjectId("59da6a511c7a9ac0b6674ff0"),
"date" : ISODate("2017-10-08T18:11:29.133Z"),
"items" : [
{
"quantity" : 1,
"price" : 47.11,
"desc" : "Item #1"
},
{
"quantity" : 2,
"price" : 42.0,
"desc" : "Item #2"
}
],
"custInfo" : "Tobias Trelle, gold customer"
}

这是按 custInfo month wise 计算总和分组的 MongoDB 查询

db.runCommand({aggregate:"order", pipeline : 
[{$match : {$and : [{"date" : {$gte : ISODate("2016-10-08T18:10:59.899Z")}},
{"date" : {$lte : ISODate("2018-10-08T18:10:59.899Z")}}]}}
,
{ "$project" : { "custInfo" : 1 ,"count" : 1 , "date" : 1 ,
"duration" : {"$month" : [ "date"]}}},
{ "$group" : { "_id" :
{ "duration" : "$duration" , "custInfo" : "$custInfo"} ,"count" : { "$sum" : 1} }}

]}//,
//cursor:{batchSize:1000}

)

请帮助我哪里错了。

问候克里斯

最佳答案

我不确定为什么 $month 在这里被认为是“持续时间”,但无论如何,您从字段变量和对 $month< 的调用中删除了一个美元符号 有点偏离。这应该有效:

{ "$project" : { "custInfo" : 1 ,
"count" : 1 ,
"date" : 1 ,
"duration" : {"$month" : "$date" } }}

关于mongodb - 无法在聚合管道上从 BSON 类型字符串转换为 Date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46635224/

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