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()
  • 相关阅读:
    Java 简单算法--打印乘法口诀(只使用一次循环)
    Java简单算法--求100以内素数
    ubuntu 16.04 chrome flash player 过期
    java 网络API访问 web 站点
    java scoket (UDP通信模型)简易聊天室
    leetcode1105 Filling Bookcase Shelves
    leetcode1140 Stone Game II
    leetcode1186 Maximum Subarray Sum with One Deletion
    leetcode31 Next Permutation
    leetcode834 Sum of Distances in Tree
  • 原文地址:https://www.cnblogs.com/Eivll0m/p/6004215.html
Copyright © 2011-2022 走看看