首页 > 其他分享 >mongo常用查询

mongo常用查询

时间:2022-10-31 16:59:24浏览次数:77  
标签:info 常用 mongo db getCollection 查询 test1009 find assets

条件查询

select * from table1 where aa=value1,bb=value2;

1.单条件查询

语法
db.getCollection('文档名').find({字段名:"字段值"})
例如
db.getCollection('assets_info_test1009').find({feature_type:"TS_BLINDWAY"})

2.多条件and查询

语法
1)使用and关键字
db.getCollection('表名').find( {$and: [ {字段1:'值1'} ,{字段2:'值2'} ... ] } )
例如
db.getCollection('assets_info_test1009').find( {$and: [ {facilitiess:'民塘路-留仙大道交叉口'} ,{feature_type:"TS_BLINDWAY"} ] } )
2)不使用关键字
语法
db.getCollection('表名').find({字段1:"值1",字段2:'值2'})
例如
db.getCollection('assets_info_test1009').find({feature_type:"TS_BLINDWAY",facilitiess:'民塘路-留仙大道交叉口'})

3.多条件or查询

select * from table where aa=value1 or bb=value2;
db.getCollection('表名').find( {$or: [ {字段1:'值1'} ,{字段2:'值2'} ... ] } )
db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )

4.大于、小于范围查询

语法

大于 gt gte
小于 lt lte

db.getCollection('表名').find({字段:{$gt:15,$lt:20}})

例如
db.getCollection('assets_info_test1009').find({length:{$gt:15,$lt:20}})

db.way_maintenance.find({insert_time:{$gt:1604046600517,$lt:1604048600517}})

5.模糊查询

db.getCollection('assets_info_test1009').find({facilitiess:/路/})
select * from table where aa like '%路%';

db.getCollection('assets_info_test1009').find({facilitiess:/^.*交叉口/})
select * from table where aa like '%交叉口';

db.getCollection('assets_info_test1009').find({facilitiess:/致远.*/})
select * from table where aa like '致远%';

指定列名

db.getCollection('表名').find({查询条件字段:"值"},{列名1:1,列名2:1,...})

db.getCollection('assets_info_test1009').find({feature_type:"TS_BLINDWAY"},{adcode:1,GUID:1})

count 汇总

db.getCollection('assets_info_test1009').find({length:{$gt:15,$lt:20}}).count()

group by

注意group中一定要有_id,_id后面的字段名相当于sql的group by后面的字段名

点击查看代码
db.getCollection('assets_info_test1009').aggregate([
    {
        $match:{
            'feature_type':'TS_CURBSTONE'
            }
     },
     {
        $group:{
                _id:"$feature_type",
                sumLength:{
                    $sum:"$length"
                    }
                }
            
    }
])

排序

-1:逆序
1:正序
db.getCollection('assets_info_test1009').find({facilitiess:/致远.*/}).sort({length:-1})
db.getCollection('assets_info_test1009').find({facilitiess:/致远.*/}).sort({length:1})

distinct

  • 字段名需要用引号
    db.getCollection('表名').distinct('字段名',{查询条件})
    db.getCollection('assets_info_test1009').distinct('feature_type',{length:{$gt:15,$lt:20}})

可参考blog:
Mongo db 与mysql 语法比较

标签:info,常用,mongo,db,getCollection,查询,test1009,find,assets
From: https://www.cnblogs.com/xiaoyujade/p/16836713.html

相关文章

  • 现代密码学常用符号总结
    本文将总结现代密码学(ModernCryptography)中的常见数学符号,了解以下预备知识可以极大增加本文的阅读体验:离散数学,线性代数与概率论三门课程中的主要数学记号及......
  • RDD(弹性分布式数据集)及常用算子
    RDD(弹性分布式数据集)及常用算子RDD(ResilientDistributedDataset)叫做弹性分布式数据集,是Spark中最基本的数据处理模型。代码中是一个抽象类,它代表一个弹性的、不可......
  • Sequelize查询条件限制
    Sequelize查询条件限制查询条件限制constOp=Sequelize.Op[Op.and]:{a:5}//且(a=5)[Op.or]:[{a:5},{a:6}]//(a=5或a=6)[Op.gt]:6......
  • 常用命令
    gitconfig--globaluser.name"YourName"//设置git用户名和邮箱[email protected] 1.npm,yarn,pnpm的设置npmsetregistryh......
  • java-Swing常用组件-1
    文章目录​​Swing常用组件​​Swing常用组件......
  • SQL查询(单表查询)
     目录​​目标​​​​前期准备:​​​​基础数据:​​​​简单查询:(这是直接条件直接复制在自己的编辑器里,自己试着练习)​​​​条件查询:​​​​排序查询:​​​​聚合查询:(......
  • IDEA常用快捷键(持续更新)
    Ctrl类Ctrl+Y 删除当前行Ctrl+D复制当前行Ctrl+/添加注释和取消注释Ctrl+alt+L快速格式化代码Ctrl+H 查看类的层级关系Ctrl+B......
  • 完成全查询商品(ssm框架)
    目录结构:  注意: 标记的数字,是本次需要修改的地方,分为(1,2,3,4,5,6)。​​ssm框架的搭建​​,我在上一篇博客有详细的介绍,这里就不详细介绍了。实现功能:思路分析数据库准备......
  • 框架的常用配置(一直更新)
     框架配置:web.xml配置:<!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd"><web-app><di......
  • linux常用命令(一【公开版】)
    目录​​常用的:​​​​help:(2种)​​​​文件和目录常用命令​​​​LS:​​​​cd:​​​​touch:​​​​rm: ​​​​拷贝和移动文件:​​​​查看文件内容:​​​​c......