如何解决添加另一个字段时,MongoDb聚合组和总和给出了怪异的数字
我想获得按年,月和州划分的交易总数。 如果我按年和月分组和求和-我会得到正确的数字。
当我添加“ state”时,数字关闭。很多。
这是查询-注释掉了“状态”,因此我不必粘贴两次-只需取消注释行即可为您提供我正在尝试的内容,给出错误的数字。
结果如下-
起初,我虽然有数百条记录缺少“ address.state”字段-但是对其进行搜索导致仅1(一条)记录缺少地址对象。
.find({'address.state':{ $exists: false }})
汇总查询-“状态”已被注释掉...
db.getCollection('transactions').aggregate([
{
$match: { type: 'sale' }
},{
$project: {
year: { $year: '$dates.transaction' },month: { $month: '$dates.transaction' },//state: '$address.state'
}
},{
$group: {
_id: {
year: '$year',month: '$month',//state: '$state'
},count: { $sum: 1 }
}
},{ $sort: { '_id.year': 1 } },{ $sort: { '_id.month': 1 } },//{ $sort: { '_id.state': 1 } },{
$project: {
'year': '$_id.year','month': '$_id.month',//'state': '$_id.state','count': '$count',_id:0
}
}
]);
这是过去一年的结果
您可以看到-前4列包含状态数据-而且我显然有很多记录缺少状态数据-但更重要的是-MONTHS总数的数字甚至不匹配,年度总数也不
我非常有信心右侧的3列分组(总和不包括“ state”分组)。
对我做错了什么了解?
每个请求,这里有一些数据。
-
这是实际数据,我已经能够仅使用此数据和上述查询来重新创建问题-缺少的记录来自于TN,TX和VA状态。我将这些记录放在此数据转储的底部-对其他记录进行计数并将其分组在一起。 (实际上-由于职位限制,不得不减少);因此我删除了一堆“空白状态”记录,留下了大量的“好”记录和所有“坏”(缺失)记录-我看不到为什么不对它们进行分组或计数-任何帮助...请。
/ * 1 * / { “ _id”:ObjectId(“ 5e0bf3bba68d2f0017303f70”), “ address”:{“ city”:“”,“ state”:“”,“ zip”:“ 37085”}, “ buyer”:“ Mykaela”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-01T01:19:55.857Z”)} }
/ * 94 * / { “ _id”:ObjectId(“ 5e33872a4393d30017ad3739”), “ address”:{“ city”:“”,“ state”:“”,“ zip”:“ 15229”}, “ buyer”:“ Emily”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-31T01:47:22.058Z”)} }
/ * 34 * / { “ _id”:ObjectId(“ 5e1df85253aff80017883b8e”), “ address”:{“ city”:“ Anniston”,“ state”:“ AL”,“ zip”:“ 36207”}, “ buyer”:“ Marshall”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-14T17:20:18.875Z”)} }
/ * 32 * / { “ _id”:ObjectId(“ 5e1d2920fe0d0900177f5d38”), “ address”:{“ city”:“ Alexander”,“ state”:“ AR”,“ zip”:“ 72002”}, “ buyer”:“ Jonathon”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-14T02:36:16.613Z”)} }
/ * 13 * / { “ _id”:ObjectId(“ 5e14ec7e93f30c00170f2634”), “ address”:{“ city”:“ Armuchee”,“ state”:“ GA”,“ zip”:“ 30105”}, “ buyer”:“ Shiloh”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-07T20:39:26.918Z”)} }
/ * 80 * / { “ _id”:ObjectId(“ 5e2f97173f8cce00176d64c2”), “ address”:{“ city”:“ MACON”,“ state”:“ GA”,“ zip”:“ 31220”}, “ buyer”:“ James”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-28T02:06:15.562Z”)} }
/ * 62 * / { “ _id”:ObjectId(“ 5e2791773511420017540bc9”), “ address”:{“ city”:“ Painted Post”,“ state”:“ NY”,“ zip”:“ 14870”}, “ buyer”:“ Ethan”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-22T00:04:07.701Z”)} }
/ * 52 * / { “ _id”:ObjectId(“ 5e23a45b3c46750017f65191”), “ address”:{“ city”:“ Walhalla”,“ state”:“ SC”,“ zip”:“ 29691”}, “ buyer”:“ Richard”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-19T00:35:39.967Z”)} }
/ * 74 * / { “ _id”:ObjectId(“ 5e2e203fbf31900017fa444b”), “ address”:{“ city”:“ Chapin”,“ state”:“ SC”,“ zip”:“ 29036”}, “ buyer”:“ Preston”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-26T23:26:55.813Z”)} }
/ * 21 * / { “ _id”:ObjectId(“ 5e18e4db14400f001737172f”), “ address”:{“ city”:“ Nairobi”,“ state”:“ TN”,“ zip”:“ 00100”}, “ buyer”:“ Gregory”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-10T20:55:55.239Z”)} }
/ * 4 * / { “ _id”:ObjectId(“ 5e0cc53c137f2a0017d2f8ca”), “ address”:{“ city”:“ Waxahachie”,“ state”:“ TX”,“ zip”:“ 75167”}, “ buyer”:“ Acacia”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-01T16:13:48.014Z”)} }
/ * 57 * / { “ _id”:ObjectId(“ 5e266a66cd3e010017f50fd5”), “ address”:{“ city”:“ spring”,“ state”:“ TX”,“ zip”:“ 77379”}, “ buyer”:“ Melanie”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-21T03:05:10.442Z”)} }
/ * 63 * / { “ _id”:ObjectId(“ 5e28b3b13511420017540bfb”), “ address”:{“ city”:“ Abilene”,“ state”:“ TX”,“ zip”:“ 79605”}, “ buyer”:“ Isreal”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-22T20:42:25.366Z”)} }
/ * 92 * / { “ _id”:ObjectId(“ 5e32d0d13f8837001779160e”), “ address”:{“ city”:“ glenn heights”,“ state”:“ TX”,“ zip”:“ 75154”}, “ buyer”:“ Kohron”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-30T12:49:21.432Z”)} }
/ * 45 * / { “ _id”:ObjectId(“ 5e20c621c5fb2700178d2b4e”), “ address”:{“ city”:“ Barboursville”,“ state”:“ VA”,“ zip”:“ 22923”}, “ buyer”:“ Harrison”, “ type”:“销售”, “ date”:{“ transaction”:ISODate(“ 2020-01-16T20:22:57.061Z”)} }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。