一、好言
心酸縱有千百種,沉默不語(yǔ)最難過(guò)。
二、背景
產(chǎn)品要我查詢(xún)數(shù)據(jù),結(jié)果一看數(shù)據(jù)存在mongodb里面,咔嚓,mongodb的語(yǔ)法我還真沒(méi)寫(xiě)過(guò),所以就邊搜邊查詢(xún)了,順便記錄下。
三、語(yǔ)句
3.1、查詢(xún)時(shí)間段:
db.UserRecord.find({createTime:{'$gte':new Date('2017/05/21
08:00:00'),'$lte':new Date('2017/05/22
08:00:00')},id:123}).sort({createTime:1}).count()
說(shuō)下查詢(xún)時(shí)間的問(wèn)題,轉(zhuǎn)換時(shí)間時(shí)候需要向加八小時(shí),所以上面是查詢(xún)5月21號(hào)的數(shù)據(jù),并且按照時(shí)間排序,1是升序,-1是降序
db.集合.find({column:param})的形式。
3.2 、查詢(xún)id=123的數(shù)據(jù)
db.getCollection('UserRecord').find({id:123});
3.3 、查詢(xún)id=123的數(shù)據(jù)的總數(shù)
db.getCollection('DialogDeviceUserRecord').find({id:123}).count();
var arr = db.getCollection('UserRecord').find({});
while(arr.hasNext()) db.UserRecord.insert(arr.next());
mongoexport -uroot -pxxxxxx --authenticationDatabase=admin -d device -c UserRecord --type=csv -f device -o /tmp/1.txt
db.UserRecord.ensureIndex({'userid':1, 'no':1}, {background:true})
查詢(xún)索引:
db.getCollection('test').getIndexes()