添加另一个字段时,MongoDb聚合组和总和给出了怪异的数字

如何解决添加另一个字段时,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
        } 
    }
]);

这是过去一年的结果

SS of year

您可以看到-前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(“ 5e2e203fbf319​​00017fa444b”), “ 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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-