查询语句
一、简单查询
1.1 按照时间区间查询:
"$gt" 、"$gte"、 "$lt"、 "$lte"(分别对应">"、 ">=" 、"<" 、"<=")
db.getCollection('image').find({ "time" : { "$gte" : ISODate("2018-12-30T00:00:00Z")
, "$lte" : ISODate("2019-02-26T23:59:59Z") } }).count()
db.CollectionAAA.find({ "CreateTime" : { "$gte" : ISODate("2017-04-20T00:00:00Z"), "$lt" : ISODate("2017-04-21T00:00:00Z") } }).count()
db.testCollOne.find({ "time":{ "$gte" :ISODate("2018-07-12T03:55:00Z"), "$lt" : ISODate("2017-07-12T03:56:00Z") } }).limit(5)
1.2 时间排序
db.getCollection('alarm').find({}).sort({opt_time:-1})
db.getCollection('image').find({}).sort({time:-1})
1.3 查询指定字段
+ db.getCollection('multobj').find({},{backgroundImage : 1}),这里就只返回backgroundImage和_id字段
1.4 某个字段存在
db.getCollection('multobj').find({code:{$exists:true}})
二、模糊查询
2.1 前缀匹配
db.getCollection('testxn').find({plateNumber:/^123/}).limit(100)
2.2 后缀匹配
db.getCollection('testxn').find({plateNumber:/123^/}).limit(100)
2.3 模糊匹配
db.getCollection('testxn').find({plateNumber:/123/}).limit(100)
2.4 注意
三、distinct
- 查询一个字段的所有取值
- db.getCollection(‘multobj’).distinct(“areaId”);注意数据量大时如果字段没有索引可能会执行非常久。