mongo常用查询

条件查询

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 语法比较

posted on 2022-10-31 16:45  小金鱼的笔记  阅读(380)  评论(0)    收藏  举报