zoukankan      html  css  js  c++  java
  • 日常工作中使用的一些Mongodb语句

    1、通过_userID字段查询重复录入内容
    > db.template.aggregate({"$group": {_id: "$_userId",count:{"$sum":1}}},{$sort:{count:-1}})
    
    2、查询
    db.template.find({"group_7ee1247ab7194482b86f82db556d8580.sex":"男"}).pretty()
    db.template.find({"group_7ee1247ab7194482b86f82db556d8580.sex":""}).pretty()
    
    3、删除指定的文档
    db.template.remove({"_id" : "1467983324494"})
    db.template.remove({"_id" : "1467983775108"})
    
    4、更新数组中的嵌套文档键值
    db.template.update({"_id" : "1467979132090","group_7ee1247ab7194482b86f82db556d8580.sex":""},{"$set":{"group_7ee1247ab7194482b86f82db556d8580.$.sex":""}})
    
    5、按时间排序,查找新最新插入的数据
    db.template.aggregate({$project:{starttime:"$starttime"}},{$sort:{starttime:-1}})
    db.template.find({},{starttime:1}).sort({starttime:-1})
    
    6、分组统计,按年龄分组
    db.template.aggregate({$project:{group_7ee1247ab7194482b86f82db556d8580:"$group_7ee1247ab7194482b86f82db556d8580"}}, {$unwind:"$group_7ee1247ab7194482b86f82db556d8580"},{$group: {_id: "$group_7ee1247ab7194482b86f82db556d8580.age",count:{"$sum":1}}},{$sort:{count:-1}})
    
    7、删除数组嵌套文档中的键和值
    db.template.update({ "group_7ee1247ab7194482b86f82db556d8580.tel": "15835375578"},{ $unset: { "group_7ee1247ab7194482b86f82db556d8580.$.tel" : 1 }})
    
    8、如果查询结果集较多,默认一次只能显示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()
  • 相关阅读:
    二分查找:思路很简单,细节是魔鬼
    今天遇到了 X-Y PROBLEM
    《工匠精神》读书笔记
    养成好的职业习惯
    php xdebug 调试
    Dockerfile的常见命令
    docker制作镜像
    脱坑笔记
    Java中代理
    2、遇到多个构造器参数的时候要考虑使用构建器
  • 原文地址:https://www.cnblogs.com/Eivll0m/p/6004215.html
Copyright © 2011-2022 走看看