mogodbshell中数组对象查询修改方法
在mongodb中,存在如下数据
{ "_id" : ObjectId("59af55078a8fc5e51ff425de"), "title" : "title1", "col" : "col
", "reader" : [ { "readername" : "jim", "isread" : true }, { "readername" : "ka
te" }, { "readername" : "lilei" } ], "begindate" : "Wed Sep :: GMT
+ (中国标准时间)" }
{ "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col
", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern
ame" : "lilei" }, { "readername" : "lily" } ], "begindate" : "Wed Sep
:: GMT+ (中国标准时间)" }
{ "_id" : ObjectId("59af55458a8fc5e51ff425e0"), "title" : "title3", "col" : "col
", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" } ], "beginda
te" : "Wed Sep :: GMT+ (中国标准时间)" }
需求1:查询栏目是col1,且读者是lily的记录:
> db.articles.find({col:'col1','reader.readername':'lily'})
//查询结果
{ "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col
", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern
ame" : "lilei" }, { "readername" : "lily" } ], "begindate" : "Wed Sep
:: GMT+ (中国标准时间)" }
即数组中的对象用形如“数组名.字段”组成
需求2:把标题为title2,且读者为lily的已读记录‘isread’设置为true
> db.articles.update({title:'title2','reader.readername':'lily'},{$set:{'reader.
$.isread':true}})
WriteResult({ "nMatched" : , "nUpserted" : , "nModified" : })
{ "_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
", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern
ame" : "lilei" }, { "readername" : "lily", "isread" : true } ], "begindate" : "W
ed Sep :: GMT+ (中国标准时间)" } //执行命令
> db.articles.update({title:'title2','reader.readername':'lilei'},{$pull:{'reader':{readername:'lilei'}}}) //执行命令后查询的结果
> db.articles.find({title:'title2'});
{ "_id" : ObjectId("59af552e8a8fc5e51ff425df"), "title" : "title2", "col" : "col
", "reader" : [ { "readername" : "jim" }, { "readername" : "kate" }, { "readern
ame" : "lily", "isread" : true } ], "begindate" : "Wed Sep :: GMT+
(中国标准时间)" }
核心是用$pull命令查找数组名称,然后通过属性值删除数组内的对象记录
mogodbshell中数组对象查询修改方法的更多相关文章
- js中数组对象去重的方法
var arr = [{ key: '01', value: '乐乐' }, { key: '02', value: '博博' }, { key: '03', value: '淘淘' },{ key: ...
- 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)
方法一:使用Arrays.asList()方法 1 2 String[] asset = {"equity", "stocks", "gold&q ...
- 总结Javascript中数组各种去重的方法
相信大家都知道网上关于Javascript中数组去重的方法很多,这篇文章给大家总结Javascript中数组各种去重的方法,相信本文对大家学习和使用Javascript具有一定的参考借鉴价值,有需要的 ...
- java中数组有没有length()方法?string没有lenght()方法?
java中数组有没有length()方法,求数组的长度可以使用数组的length属性. int[] arr={1,2,3,4,5}; int length=arr.length;//求数组的长度 -- ...
- 【Java面试题】18 java中数组有没有length()方法?string没有lenght()方法?下面这条语句一共创建了多少个对象:String s="a"+"b"+"c"+"d";
数组没有length()这个方法,有length的属性.String有有length()这个方法. int a[]; a.length;//返回a的长度 String s; s.length();// ...
- 微信小程序中如何使用setData --- 修改数组对象、修改对象
看代码吧~ 这是修改对象 this.setData({ allStageIndex: e.detail.value, [`projectDetailsData.stage`]: this.data.a ...
- js 中数组对象的定义赋值 以及方法
1.定义数组 var m=new Array(); var n=[]; 2.数组的赋值(两种) A. var m=new Array(2); 一个值表示数组length var m=new Array ...
- javascript中数组的22种方法
× 目录 [1]对象继承 [2]数组转换 [3]栈和队列[4]数组排序[5]数组拼接[6]创建数组[7]数组删改[8]数组位置[9]数组归并[10]数组迭代[11]总结 前面的话 数组总共有22种方法 ...
- javascript中数组的22种方法 (转载)
前面的话 数组总共有22种方法,本文将其分为对象继承方法.数组转换方法.栈和队列方法.数组排序方法.数组拼接方法.创建子数组方法.数组删改方法.数组位置方法.数组归并方法和数组迭代方法共10类来进行详 ...
随机推荐
- pushbutton成为可点击的图标(实现全透明,不论点击与否都只显示Icon)(也就是一个万能控件)
需求 需要2个按钮,一个是音乐开关,一个是关闭窗口,此文章关闭pushButton的透明问题(hovered+pressed都不会有背景色和边框的变化) 原理 使窗口完全透明 代码 _pPushBut ...
- Vue插件资料
UI组件element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和WeUI的组件库 iview ★5801 - 基于 Vuejs 的开源 UI ...
- npm与cnpm混用导致的问题
npm和cnpm混用之后,再用npm升级模块导致如下错误: 解决办法: 删除node_modules文件下的文件后,重新执行npm install
- WPF UserControl 的绑定事件、属性、附加属性
原文:WPF UserControl 的绑定事件.属性.附加属性 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Vblegend_2013/arti ...
- 新建py文件时取名千万要小心 不要和已有模块重名
这是因为我新建了一个email.py的文件 后来我将文件名rename成了myemail.py没有看改名提示,结果导致所有的对email的import和调用全部改成了对myemail的import和调 ...
- .net core下,Ocelot网关与Spring Cloud Gateway网关的对比测试
有感于 myzony 发布的 针对 Ocelot 网关的性能测试 ,并且公司下一步也需要对.net和java的应用做一定的整合,于是对Ocelot网关.Spring Cloud Gateway网关做个 ...
- 4 DDD里面的界限上下文
1 界限上下文概念的出现 当开发一个电子商务系统的时候,会给系统划分很多子域,销售子域是核心子域,此外还有物流子域,商品子域等支撑子域.在这些子域里面,一个商品product在销售子域和商品子域里面外 ...
- windows下Redis 主从读写分离部署
原文:windows下Redis 主从读写分离部署 1.可直接下载window下的运行文件(下面这个链接) 也可以浏览github 查看相应的版本说明文档 https://github.com/Ser ...
- vi学习(1)
今天下午看了vi频繁使用的操作,现在记录,为了方便日后查询. 按vi模式.进入命令3部分. (一) 一般模式下 字符操作:上下左右箭头(或kjhl)能够实现光标上下左右移动一位. 假设想要进行多次移动 ...
- python3 基本使用多线程
#coding=utf-8 import threading #进口threading from time import sleep import time def task1(): print (& ...