日常工作中使用的一些Mongodb语句

2023-04-20,,

、通过_userID字段查询重复录入内容
> db.template.aggregate({"$group": {_id: "$_userId",count:{"$sum":}}},{$sort:{count:-}}) 、查询
db.template.find({"group_7ee1247ab7194482b86f82db556d8580.sex":"\b男"}).pretty()
db.template.find({"group_7ee1247ab7194482b86f82db556d8580.sex":"女 "}).pretty() 、删除指定的文档
db.template.remove({"_id" : ""})
db.template.remove({"_id" : ""}) 、更新数组中的嵌套文档键值
db.template.update({"_id" : "","group_7ee1247ab7194482b86f82db556d8580.sex":"女"},{"$set":{"group_7ee1247ab7194482b86f82db556d8580.$.sex":"男"}}) 、按时间排序,查找新最新插入的数据
db.template.aggregate({$project:{starttime:"$starttime"}},{$sort:{starttime:-}})
db.template.find({},{starttime:}).sort({starttime:-}) 、分组统计,按年龄分组
db.template.aggregate({$project:{group_7ee1247ab7194482b86f82db556d8580:"$group_7ee1247ab7194482b86f82db556d8580"}}, {$unwind:"$group_7ee1247ab7194482b86f82db556d8580"},{$group: {_id: "$group_7ee1247ab7194482b86f82db556d8580.age",count:{"$sum":}}},{$sort:{count:-}}) 、删除数组嵌套文档中的键和值
db.template.update({ "group_7ee1247ab7194482b86f82db556d8580.tel": ""},{ $unset: { "group_7ee1247ab7194482b86f82db556d8580.$.tel" : }}) 、如果查询结果集较多,默认一次只能显示20条左右,使用以下方法可以一次性打印
db.foo.find().toArray()

db.foo.find().forEach(printjson) 9、查询指定字段(表结构比较复杂,使用聚合框架,先投射、再进行两次拆分、然后匹配、最后再投射一次)
db.szh.aggregate({$project:{"result.paramitems":"$result.paramitems"}},{$unwind:"$result.paramitems"},{$unwind:"$result.paramitems.items"},{$match:{"result.paramitems.items.name":"车型名称"}},{$project:{"result.paramitems.items.modelexcessids":1}}).pretty()

日常工作中使用的一些Mongodb语句的相关教程结束。

《日常工作中使用的一些Mongodb语句.doc》

下载本文的Word格式文档,以方便收藏与打印。