mogodbshell中数组对象查询修改方法
在mongodb中,存在如下数据
{ "_id" : ObjectId("59af55078a8fc5e51ff425de"), "title" : "title1", "col" : "col 1", "reader" : [ { "readername" : "jim", "isread" : true }, { "readername" : "ka te" }, { "readername" : "lilei" } ], "begindate" : "Wed Sep 06 2017 09:53:11 GMT +0800 (中国标准时间)" } { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern ame" : "lilei" }, { "readername" : "lily" } ], "begindate" : "Wed Sep 06 2017 09 :53:50 GMT+0800 (中国标准时间)" } { "_id" : ObjectId("59af55458a8fc5e51ff425e0"), "title" : "title3", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" } ], "beginda te" : "Wed Sep 06 2017 09:54:13 GMT+0800 (中国标准时间)" }
需求1:查询栏目是col1,且读者是lily的记录:
> db.articles.find({col:'col1','reader.readername':'lily'}) //查询结果 { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern ame" : "lilei" }, { "readername" : "lily" } ], "begindate" : "Wed Sep 06 2017 09 :53:50 GMT+0800 (中国标准时间)" }
即数组中的对象用形如“数组名.字段”组成
需求2:把标题为title2,且读者为lily的已读记录‘isread’设置为true
> db.articles.update({title:'title2','reader.readername':'lily'},{$set:{'reader. $.isread':true}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern ame" : "lilei" }, { "readername" : "lily", "isread" : true } ], "begindate" : "W ed Sep 06 2017 09:53:50 GMT+0800 (中国标准时间)" }
核心是$,可以理解为数组定位器
需求3:删除名为title2下的reader名字为lilei的读者对象:
//未删除前数据 { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern ame" : "lilei" }, { "readername" : "lily", "isread" : true } ], "begindate" : "W ed Sep 06 2017 09:53:50 GMT+0800 (中国标准时间)" }//执行命令 > db.articles.update({title:'title2','reader.readername':'lilei'},{$pull:{'reader':{readername:'lilei'}}})//执行命令后查询的结果 > db.articles.find({title:'title2'}); { "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col 1", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern ame" : "lily", "isread" : true } ], "begindate" : "Wed Sep 06 2017 09:53:50 GMT+ 0800 (中国标准时间)" }
核心是用$pull命令查找数组名称,然后通过属性值删除数组内的对象记录
转载于:https://www.cnblogs.com/qkabcd/p/7483357.html
mogodbshell中数组对象查询修改方法相关推荐
- 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)
方法一:使用Arrays.asList()方法 1 2 String[] asset = {"equity", "stocks", "gold&quo ...
- Oracle数据库表中字段顺序的修改方法
Oracle数据库表中字段顺序的修改方法 这篇文章主要给大家介绍了关于Oracle数据库表中字段顺序的修改方法,在介绍修改的方法之前先给大家介绍了Oracle数据库表新建字段的方法,文中通过示例代码介 ...
- 数组对象的reduce方法
数组对象的reduce方法 const array1 = new Array(1, 2, 3, 4);const array2 = new Array(3, 4, 5, 6);// 求 array1 ...
- 数组对象的filter方法
数组对象的filter方法 const array1 = new Array(1, 2, 3, 4);const array2 = array1.filter(function (value, ind ...
- java中字符串和数组如何比较_[Java教程]javascript中数组和字符串的方法比较
[Java教程]javascript中数组和字符串的方法比较 0 2016-07-19 23:00:05 ×目录[1]可索引 [2]转换 [3]拼接[4]创建[5]位置 前面的话 字符串和数组有很多的 ...
- java中数组的length_java中数组有没有length()方法?string没有lenght()方法?
展开全部 java中数组是没有length()方法的,只有length属性,数组array.length返回62616964757a686964616fe4b893e5b19e313333663064 ...
- layui数据表格自定义复选框表头_layui中table表头样式修改方法
如下所示: layui.use('table', function () { var table = layui.table; table.render({ elem: '#desTable' , u ...
- layui表头样式_layui中table表头样式修改方法
如下所示: layui.use('table', function () { var table = layui.table; table.render({ elem: '#desTable' , u ...
- java 数组 length 减少_java中数组有没有length()方法?string没有lenght()方法?
java中数组有没有length()方法,求数组的长度可以使用数组的length属性. int length=arr.length;//求数组的长度 ------------------------- ...
最新文章
- 通用的底层埋点都是怎么做的?
- 闽江夜话:聆听清华之声,畅聊数字中国的未来
- 如何实现一个优质的微服务框架:Apache ServiceComb 的开放性设计
- Win32 ASM 简单对话框编程Demo
- CSS样式让一个块在最上层中显示
- nova 之compute服务
- oracle dg apply lag,Oracle数据库 DGbroker三种保护模式的切换
- c语言 overflow_一文读懂:C语言编程中“堆”和“栈”的七大区别
- 4.linux基础1
- paip.最省内存的浏览器评测 cah
- JVM-内存区域与OOM
- 什么是Vue全家桶,Vue全家桶包含哪些东西以及怎么使用
- 2021-11-03 - 英文/英语简写 - 收集
- 微信小程序开发BUG经验总结
- QT+讯飞智能语音在线识别demo,录音识别
- InputStream和FileInputStream
- Fedora17下的hadoop-1.0.4系统配置
- 操作系统——(12)保护和安全
- 我们的节日| 包汤圆、猜灯谜,喜迎元宵佳节
- VUE制作周排班样式