数据库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.user.find({state_arr:{$elemMatch:{$eq:"123"}}})  查询数组

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

聚合查询:

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}}}])

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

开启慢查询日志:

./mongo --host:127.0.0.1:27017

1:通过mongo shell:
#查看状态:级别和时间
drug:PRIMARY> db.getProfilingStatus()
{ "was" : 1, "slowms" : 100 }
#查看级别
drug:PRIMARY> db.getProfilingLevel()
1
#设置级别
drug:PRIMARY> db.setProfilingLevel(2)
{ "was" : 1, "slowms" : 100, "ok" : 1 }
#设置级别和时间
drug:PRIMARY> db.setProfilingLevel(1,200)
{ "was" : 2, "slowms" : 100, "ok" : 1 }

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

创建索引

db.getCollection("objects").createIndex({"ownerId":1},{"name":"idx_ownerId",background:true})

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

 

posted @ 2016-07-28 09:30  凉城  阅读(1406)  评论(0编辑  收藏  举报