zoukankan      html  css  js  c++  java
  • 数据库MongoDB查询语句--持续更新

    数据库可视化工具robomongo 下载地址

    链接:https://pan.baidu.com/s/1RjU1BXq2rXFG07Zaw5BHrQ
    提取码:o1w5

    ----------------------------------------------------------------------------------------

    模糊查询:

    包含字符串str : find({'name':/str/i}); {'name':/str/}

     以str开头:   {'name':/^str/}

    不包含,$not查询:

    "task_begin_time":{$not:/3/}

    $in查询:

    字段:{ field: { $in: [<value1>, <value2>, ... <valueN> ] }

    eg:db.inventory.find( { qty: { $in: [ 5, 15 ] } } )

      db.inventory.find( { tags: { $in: [ /^be/, /^st/ ] } } )

    db.things.find( { x : { $ne : 3 } } )

    条件相当于x<>3,即x不等于3。

    ---16-08-18新增

    大于 $gt 小于 $lt  大于等于 $gte  小于等于 $lte

    字段是否存在: db.inventory.find({x:{$exists:true}})

    排序:db.inventory.find({}).sort({x:-1});  -1:DESC倒序  1:正序ASC

    更新:db.getCollection('n.m.mobjects').update({ownerId:/6666/},{$set:{'ownerId':'6666','modifiedBy':'6666'}},{multi:true})

    ---------------------------------------------------------------------------

    日期条件用法:

    字段类型为日期:查询大于某一个日期  db.inventory.find({x:{$gt:new Date('2016-09-15')}})

    -------------------------------------------------------------------------------------------

     or  的用法:

    db.getCollection('sessions').find({'$or':[{logoffTime:{$gt:new Date('2016-09-21')}},{logoffTime:{$exists:false}}]}).sort({logonTime:-1})

    -------------------------------------------------------------------------------------------

    扩展属性查询:

    db.getCollection('sessions').find({'extraData.userId':'ACDFDFDFDF'}) //查询具体值;

    db.getCollection('sessions').find({'extraData.userId':{$exists:true}}) //查询是否存在字段

    ---------------------------------------------------------------------------------------------------------------------

    limit用法:

    db.getCollection('sessions').find({name:/新/}).sort({createdTime:-1}).limit(1).skip(1)

    -------------------------------------------------------------------------------------------------------------------------

    update:更新多个

    db.getCollection("workitems").update({activityDefineName:'视音频',state:'Exception'},{$set:{'state':'Completed'}},{multi:true});

    --------------------------------------------------------------------------------------------------------------------------

     字段的隐藏展示:

    db.getCollection('sessions').find({name:/新/},{_id:0,name:1}).sort({createdTime:-1})

    ---------------------------------------------------------------------------------------------------------------------------

    文档数据的删除:

    db.getCollection('sessions').remove({'id':'12321'});

     db.getCollection('sessions').deleteMany({});  删除全部符合条件的文档;

    db.getCollection('sessions').deleteOne({}); 删除一个符合条件的文档;

    ------------------------------------------------------------------------------------------------------------------------

    找出数组中, 具有 groupId=1234并且admin=true的记录

    db.getCollection("users").find({"joinedGroups":{$elemMatch: {"groupId":"1234","admin":true}}})

    找出数组中, 具有 groupId=1234或者admin=true的记录

    db.getCollection("users").find({"joinedGroups.groupId":'1234',"joinedGroups.admin": true})

    -------------------------------------------------------------------------------------------------------------------------------

    聚合查询:

    db.getCollection('assets').aggregate([{$match:{"category":'video'}},{$group:{_id:'$ownerId',num:{$sum:1}}}])

    match是过滤,group是聚合,

    db.getCollection('sessions').aggregate([{$match:{"state":'On'}},{$group:{_id:'$userName',num:{$sum:1}}},{$match:{num:{$gt:1}}}])

    -------------------------------------------------------------------------------------------------------------------------------

    聚合操作中的其他方法
    $limit,限制结果数量

    $skip,忽略结果的数量

    $sort,按照给定的字段进行排序

    db.daily_ad_composite.aggregate([{"$match":{"date":"2017-11-27"}},
    {$group:{_id:"$appid",totalViews:{$sum:"$views"},clicks:{$sum:"$clicks"}}},
    {"$limit":50},
    {"$sort":{"date":-1}},
    {"$skip":5},
    {"$project":{"completions":1}}
    ])

    --------------------------------------------------------

    比较同一个文档,不同字段值是否相同

    db.getCollection('asset').find({"modifiedTime" : ISODate("2020-11-10T07:17:47.668Z"),
    "$where": "this.createdFrom != this.modifiedFrom"})

    ---------------------------------------------------------------------------------------

    groupby

     db.getCollection('assets').aggregate([{$match:{"modifiedBy":"SRZ"}},{$group:{_id:{"modifiedTime":"$modifiedTime","key”:"$key"},num:{$sum:1}}},{$match:{num:{$gt:1}}}])

    -------------------------------------------------------------------------------------------------

  • 相关阅读:
    pyqt信号和槽传递额外参数
    PyQt--QTreeWidget
    转载:futex同步机制详解
    Linux 下的同步机制
    Linux 下线程的理解
    Linux下的物理内存管理2-slab缓存的管理
    转:C语言的编译链接过程的介绍
    LInux中ThreadInfo中的preempt_count字段
    LInux中的物理内存管理
    Linux下的内核抢占
  • 原文地址:https://www.cnblogs.com/liangblog/p/5713508.html
Copyright © 2011-2022 走看看