公司 微信 群聊数据是通过wehub保存在mangodb数据库上面的,最近学习了一些mangodb语句,特记录在此,以备不时之需。 按时间降序查询 db . getCollection ( 'group' ). find ({}). sort ({ "insertTime"
公司微信群聊数据是通过wehub保存在mangodb数据库上面的,最近学习了一些mangodb语句,特记录在此,以备不时之需。
- 按时间降序查询
- 按时间升序排列
需要加限制
db.getCollection('message').find({}).sort({"msgTimestamp":-1}).limit(100);如果出现96代码错误
db.getCollection('message').createIndex({"key": -1}); #添加索引db.getCollection('message').getIndexes(); #查询索引
db.getCollection('message').find({}).sort({"key":1}); #按索引查询
- 双条件查询
{$and:[{"condition1":value1},{"condition2":value2}]}
)
例如
db.getCollection('groupMember').find({$and:[{"groupId": "22056076470@chatroom"}, {"updateTime":0}]}); #查询groupId为22056076470@chatroom且updateTime为0的记录
- 查询时间跨度数据
{"$match": {
"msgTimestamp":{"$gte": startTime, "$lte": endTime}
}
},
{"$group" : {
"_id":"$groupId", "count":{"$sum":1}
}
}
]) #查询当天消息
- 正则查询
- 筛选字段
db.getCollection("message").find({}, {msg: 0}); #不显示msg,其余都显示
- 既筛选又选择显示还排序
注:在python中只能使用列表进行排序,不能使用字典
message = db["message"].find({"msg":{"$regex":"?$"}}, { "_id": 0, "msgTimestamp": 1, "msg": 1}).sort([("msgTimestamp",-1)]) #仅保留msg字段并按时间戳降序排列- or