Mongodb操作语法大全
1.db.collection_name.find();
该句为查询该连接名下所有数据,collection_name为连接名
2.db.集合名.find({'_id':'6edc8b4f127411acb78bb221023e04b6'}, {'_id' : 1, 'personId' : 1});
该句为加条件查询,第一个{}为查询条件,意为集合名连接下_id为6edc8b4f127411acb78bb221023e04b6的数据会被查询出来,第二个{}为要显示哪些列,1为显示0为不显示。
3.db.集合名.find({'_id':'6edc8b4f127411acb78bb221023e04b6','personId':'p140727173310000'}, {'_id' : 1, 'personId' : 1});
该句为条件查询中的and查询,意为集合名连接下id为6edc8b4f127411acb78bb221023e04b6并且personId为p140727173310000的数据会被查询出来,并且只显示_id列和personId列。
4.db.集合名.find({'$or':[{'_id':'6edc8b4f127411acb78bb221023e04b6'},{'personId':'p140727131010027'}]});
该句为条件查询中的or查询,意为集合名连接下id为6edc8b4f127411acb78bb221023e04b6或者personId为p140727131010027的数据会被查询出来,并且只显示_id列和personId列。
5.db.集合名.find({'deviceType' : {'$gte' : 5, '$lte' : 10}});
该句为查询条件中的>=与<=的使用,意为查询deviceType列>=5并且<=10的数据。补充:<, <=, >, >=, !=($lt, $lte, $gt, $gte, $ne)
6.db.集合名.find({'deviceType' : {'$in' : [10, 22, 26]}});
该句为查询条件中的in的使用,意为查询deviceType列为10或者22或者26的数据。补充:in, not in ($in, $nin)
7.db.集合名.find({_id:/03b550fa682a46dcfed8c6524a0405d6/});
该句为查询条件中的like的使用,意为查询_id列包含03b550fa682a46dcfed8c6524a0405d6的数据。
并且like查询支持正则表达式。
补充:
select * from users where name like "%hurry%"; --mysql写法
db.users.find({name:/hurry/}); --mongodb写法
select * from users where name like "hurry%"; --mysql写法
db.users.find({name:/^hurry/}); --mongodb写法
8.关于查询日期类型的数据:
方法一、
db.集合名.find({con_date:new Date("2018-08-13")})
db.集合名.find({con_date:ISODate("2018-08-13")})
方法二、
db.集合名.find({con_date:{'$gte':ISODate("2010-12-21"),'$lt':ISODate("2010-12-22")}})
注意:使用gte和lt形式
方法三、
db.集合名.find({con_date:{'$gte':new Date('2018/08/21 00:00:00'),'$lte':new Date('2018/08/21 23:59:59')}})
db.集合名.find({con_date:{'$gte':new Date('2018-08-21 00:00:00'),'$lte':new Date('2018-08-21 23:59:59')}})
注意:使用 g t e 与 gte与 gte与lte
方法四、
db.集合名.find({con_date:ISODate("2018-02-13T16:00:00.000Z")})
db.集合名.find({con_date:ISODate("2018-08-13")})
注意:如要查询某一天的数据,并且包含时分秒则必须使用ISODate,平常查询尽量使用ISODate。
例如:
db.getCollection('ciyunBp').find({'updateTime':{$gte:new ISODate('2015-01-28 07:21:05.000Z'),$lte:new ISODate("2015-01-28 11:30:05.000Z")}})
该句查询ciyunBp连接下updateTime在2015-01-28 07:21:05.000Z和2015-01-28 11:30:05.000Z之间的数据。
db.getCollection('ciyunBp').find({'updateTime':new ISODate('2015-01-28 07:21:05.000Z')})
该句查询ciyunBp连接下updateTime为2015-01-28 07:21:05.000Z的数据。
以下几种查询亲测无效:
db.getCollection('ciyunBp').find({'updateTime':new Date('2015-01-28 07:21:05.000Z')}) 没结果
db.getCollection('ciyunBp').find({'updateTime':new Date('2015-01-28')}) 没结果
db.getCollection('ciyunBp').find({'updateTime':{$gte:new ISODate('2015-01-28 07:21:05.000Z'),$lte:new ISODate("2015-01-28 11:30:05.000Z")}}) 没结果
db.getCollection('ciyunBp').find({'updateTime':{$gte:new ISODate('2015-01-28 07:21:05.000Z'),$lte:new ISODate("2015-01-28 11:30:05.000Z")}}) 可行
结论:new Date只精确到年月日,new ISODate精确到时分秒,所以当查询中给的参数含有时分秒时只能用new ISODate,如果只含年月日,则两个都可用。
1.双模糊查询(and和or)
select * from a where name like '%zz%' or name like '%aa%' ^/(zz)|/(aa)$
1.db.getCollection('ciyunWhr').find({'$or':[{'_id':/713/},{'_id':/712/}]})
2.db.getCollection('ciyunWhr').find({'_id':/712|713/})
select * from a where name like '%zz%' and name like '%aa%'
1.db.getCollection('ciyunWhr').find({'_id':[/6&7/]})
2.db.getCollection('ciyunWhr').find({'_id':{$regex:'235'}}) monogdb用正则表达式
3.db.getCollection('ciyunBp').find({'_id':{$regex:'.*002{1}.*35{1}.*'}})
4.db.getCollection('ciyunBp').find({'_id':{$regex:'(.*002{1}.*35{1}.*)|(.*35{1}.*002{1}.*)'}}) 不要求先后顺序
2.查询俩个时间段之间数据数目(精确到秒)