MongoDB常用查询语句有哪些?

2026-05-26 18:482阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计457个文字,预计阅读时间需要2分钟。

MongoDB常用查询语句有哪些?

公司+微信+群聊数据通过wehub保存在MongoDB数据库上,最近学习了些MongoDB语句,记录在此,以备不时之需。+按时间降序查询+db.getCollection('group').find({}).sort({ insertTime: -1 })


公司微信群聊数据是通过wehub保存在mangodb数据库上面的,最近学习了一些mangodb语句,特记录在此,以备不时之需。

MongoDB常用查询语句有哪些?

  • 按时间降序查询
db.getCollection('group').find({}).sort({"insertTime":-1});
  • 按时间升序排列
db.getCollection('group').find({}).sort({"insertTime":1});

需要加限制

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}); #按索引查询
  • 双条件查询
db.getCollection('groupMember').find(
{$and:[{"condition1":value1},{"condition2":value2}]}
)

例如

db.getCollection('groupMember').find({$and:[{"groupId": "22056076470@chatroom"}, {"updateTime":0}]}); #查询groupId为22056076470@chatroom且updateTime为0的记录
  • 查询时间跨度数据
groupmessage = db.getCollection('groupMember').aggregate([

{"$match": {
"msgTimestamp":{"$gte": startTime, "$lte": endTime}
}

},

{"$group" : {
"_id":"$groupId", "count":{"$sum":1}
}
}

]) #查询当天消息
  • 正则查询
db.getCollection('group').find({"name":{"$regex":"楼市问号"}}) #查询name字段带楼市问号的db.getCollection('message').find({"msg":{"$regex":"?$"}}) #查询msg以问号结尾的,注意中英文状态
  • 筛选字段
db.getCollection("message").find({}, {msg: 1}); #只显示msg字段
db.getCollection("message").find({}, {msg: 0}); #不显示msg,其余都显示
  • 既筛选又选择显示还排序
db.getCollection('message').find({"msg":{"$regex":"?$"}},{ "_id": 0, "msg": 1}).sort({"msgTimestamp":-1}); #仅保留msg字段并按时间戳降序排列

注:在python中只能使用列表进行排序,不能使用字典

message = db["message"].find({"msg":{"$regex":"?$"}}, { "_id": 0, "msgTimestamp": 1, "msg": 1}).sort([("msgTimestamp",-1)]) #仅保留msg字段并按时间戳降序排列
  • or
db.getCollection('message').find({$or: [{"msg":{"$regex":"?$"}}, {"msg":{"$regex":"^@群管"}}] },{"_id": 0, "msg": 1}).sort({"msgTimestamp":-1}); #mangodb版message = db["message"].find({"$or": [{"msg":{"$regex":"?$"}}, {"msg":{"$regex":"^@群管"}}] },{"_id": 0, "msg": 1}).sort([("msgTimestamp",-1)]) #python版



本文共计457个文字,预计阅读时间需要2分钟。

MongoDB常用查询语句有哪些?

公司+微信+群聊数据通过wehub保存在MongoDB数据库上,最近学习了些MongoDB语句,记录在此,以备不时之需。+按时间降序查询+db.getCollection('group').find({}).sort({ insertTime: -1 })


公司微信群聊数据是通过wehub保存在mangodb数据库上面的,最近学习了一些mangodb语句,特记录在此,以备不时之需。

MongoDB常用查询语句有哪些?

  • 按时间降序查询
db.getCollection('group').find({}).sort({"insertTime":-1});
  • 按时间升序排列
db.getCollection('group').find({}).sort({"insertTime":1});

需要加限制

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}); #按索引查询
  • 双条件查询
db.getCollection('groupMember').find(
{$and:[{"condition1":value1},{"condition2":value2}]}
)

例如

db.getCollection('groupMember').find({$and:[{"groupId": "22056076470@chatroom"}, {"updateTime":0}]}); #查询groupId为22056076470@chatroom且updateTime为0的记录
  • 查询时间跨度数据
groupmessage = db.getCollection('groupMember').aggregate([

{"$match": {
"msgTimestamp":{"$gte": startTime, "$lte": endTime}
}

},

{"$group" : {
"_id":"$groupId", "count":{"$sum":1}
}
}

]) #查询当天消息
  • 正则查询
db.getCollection('group').find({"name":{"$regex":"楼市问号"}}) #查询name字段带楼市问号的db.getCollection('message').find({"msg":{"$regex":"?$"}}) #查询msg以问号结尾的,注意中英文状态
  • 筛选字段
db.getCollection("message").find({}, {msg: 1}); #只显示msg字段
db.getCollection("message").find({}, {msg: 0}); #不显示msg,其余都显示
  • 既筛选又选择显示还排序
db.getCollection('message').find({"msg":{"$regex":"?$"}},{ "_id": 0, "msg": 1}).sort({"msgTimestamp":-1}); #仅保留msg字段并按时间戳降序排列

注:在python中只能使用列表进行排序,不能使用字典

message = db["message"].find({"msg":{"$regex":"?$"}}, { "_id": 0, "msgTimestamp": 1, "msg": 1}).sort([("msgTimestamp",-1)]) #仅保留msg字段并按时间戳降序排列
  • or
db.getCollection('message').find({$or: [{"msg":{"$regex":"?$"}}, {"msg":{"$regex":"^@群管"}}] },{"_id": 0, "msg": 1}).sort({"msgTimestamp":-1}); #mangodb版message = db["message"].find({"$or": [{"msg":{"$regex":"?$"}}, {"msg":{"$regex":"^@群管"}}] },{"_id": 0, "msg": 1}).sort([("msgTimestamp",-1)]) #python版