当前位置:网站首页>MongoDB分组查询
MongoDB分组查询
2022-04-21 14:43:00 【编程随想曲】
MongoDB分组查询
设置显示要查询的列
显示 device_id,device_name,online_status,video_diag_time
db.getCollection('vqd_result_his').find({},{device_id:1,device_name:1,online_status:1,video_diag_time:1})
不显示 device_id,其他都显示
db.getCollection('vqd_result_his').find({},{device_id:0})
设置要排序的字段
device_name 升序
db.getCollection('vqd_result_his').find({}).sort({'device_name':1})
device_name 降序
db.getCollection('vqd_result_his').find({}).sort({'device_name':-1})
分组查询
db.vqd_result_his.aggregate([{
'$group':{'_id':'$device_id',count:{$sum:1}}}])
多字段分组查询
db.vqd_result_his.aggregate([{
'$group':{'_id':{'device_id':'$device_id','online':'$online_status','device_name':'$device_name'},count:{$sum:1}}}])
条件过滤后分组查询
db.vqd_result_his.aggregate([{
'$match':
{
'$and':[
{'video_diag_time':{'$gte':ISODate("2020-08-01T00:00:00Z")}},
{'video_diag_time':{'$lte':ISODate("2020-08-26T00:00:00Z")}}
]
}},{
'$group':{'_id':{'device_id':'$device_id','online':'$online_status','device_name':'$device_name'},count:{$sum:1}}}])
查询结果转为数组
db.vqd_result_his.aggregate([{
'$match':
{
'$and':[
{'video_diag_time':{'$gte':ISODate("2020-08-01T00:00:00Z")}},
{'video_diag_time':{'$lte':ISODate("2020-08-26T00:00:00Z")}}
]
}},{
'$group':{'_id':{'device_id':'$device_id','online':'$online_status','device_name':'$device_name'},count:{$sum:1}}}]).toArray()
创建列索引
db.vqd_result_his.createIndex({"device_name": -1}),此处 -1 代表倒序,1 代表正序;
版权声明
本文为[编程随想曲]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1985405
边栏推荐
- 如何申请免费SSL证书?宝塔面板SSL证书安装部署完整教程
- 数字化时代,SaaS软件如何成为国产化替代的轻骑兵?
- 如何在excel中插入文件?Excel插入对象和附件有什么区别?(插入对象能直接显示内容,但我没显示?)
- VMware Horizon 8 2111 部署系列(十三)创建应用程序池
- toString的使用与包装类的使用
- 使用枚举做的红绿灯,有界面
- 浅谈NFT的注意力经济
- 理性看待自动化测试优缺点
- Insect space-time complexity
- Experience and guidance of seniors preparing for the postgraduate entrance examination of resources and environment of Dalian University of technology in 2023
猜你喜欢

Mysql 执行流程

MySQL download and installation tutorial

DBeaver无法连接数据库,如何解决?

软件测试(三)p51-p104 软件测试用例方法、缺陷

解锁OpenHarmony技术日!年度盛会,即将揭幕!

英特尔最新成果:制造出大规模硅基量子比特

Software testing has been in office for 2 months and wants to resign

It's been 2 years since the career change test. Give some advice to girls who are still hesitating

从技术原理、主流平台、市场展望快速入门NFT

Mysql database (2)
随机推荐
word输入公式快捷键
玩转ABP框架——翻译《Mastering ABP Framework》
文本处理——sed
Insect stack
Mysql数据库(3)
Insect 1602
Insecte dans Hill
The use of toString and wrapper class
What is the reason for this? Is there anything about MySQL installation
如何申请免费SSL证书?宝塔面板SSL证书安装部署完整教程
异步操作之后让await后续的代码能够继续执行
Insect binary tree
pyspark中RDD和DataFrame之间的转换利用RDD处理DataFrame:数据分段等功能实现
Insect binary tree OJ
android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
如何在代码层面提供CPU分支预测效率
[cloud based co creation] Huawei cloud database - basic knowledge
转行测试2年了,给还在犹豫的女生一点建议
DABDetr论文解读+核心源码解读
Distributed database -- Plan hint of SQL optimization