前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[置顶] 数据库MongoDB查询语句--持续更新

[置顶] 数据库MongoDB查询语句--持续更新

作者头像
JQ实验室
发布2022-01-11 12:04:50
3.8K0
发布2022-01-11 12:04:50
举报
文章被收录于专栏:实用技术

数据库可视化工具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})

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-07-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档