在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中数组对象查询修改方法的更多相关文章

  1. js中数组对象去重的方法

    var arr = [{ key: '01', value: '乐乐' }, { key: '02', value: '博博' }, { key: '03', value: '淘淘' },{ key: ...

  2. 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)

    方法一:使用Arrays.asList()方法   1 2 String[] asset = {"equity", "stocks", "gold&q ...

  3. 总结Javascript中数组各种去重的方法

    相信大家都知道网上关于Javascript中数组去重的方法很多,这篇文章给大家总结Javascript中数组各种去重的方法,相信本文对大家学习和使用Javascript具有一定的参考借鉴价值,有需要的 ...

  4. java中数组有没有length()方法?string没有lenght()方法?

    java中数组有没有length()方法,求数组的长度可以使用数组的length属性. int[] arr={1,2,3,4,5}; int length=arr.length;//求数组的长度 -- ...

  5. 【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();// ...

  6. 微信小程序中如何使用setData --- 修改数组对象、修改对象

    看代码吧~ 这是修改对象 this.setData({ allStageIndex: e.detail.value, [`projectDetailsData.stage`]: this.data.a ...

  7. js 中数组对象的定义赋值 以及方法

    1.定义数组 var m=new Array(); var n=[]; 2.数组的赋值(两种) A. var m=new Array(2); 一个值表示数组length var m=new Array ...

  8. javascript中数组的22种方法

    × 目录 [1]对象继承 [2]数组转换 [3]栈和队列[4]数组排序[5]数组拼接[6]创建数组[7]数组删改[8]数组位置[9]数组归并[10]数组迭代[11]总结 前面的话 数组总共有22种方法 ...

  9. javascript中数组的22种方法 (转载)

    前面的话 数组总共有22种方法,本文将其分为对象继承方法.数组转换方法.栈和队列方法.数组排序方法.数组拼接方法.创建子数组方法.数组删改方法.数组位置方法.数组归并方法和数组迭代方法共10类来进行详 ...

随机推荐

  1. Mac下cocos2dx3.1用Cocos IDE写的Lua binding篇01

    本人Lua新手,写着玩玩. 新建一个模版project. 一.改动main.lua require "Cocos2d" require "Cocos2dConstants ...

  2. Method of Seamless Integration and Independent Evolution of Information-Centric Networking via Software Defined Networking

    A method of transferring data between a software defined network (SDN) and an information-centric ne ...

  3. C#或者WPF中让某个窗体置顶

    原文:C#或者WPF中让某个窗体置顶 前记:在工作中有个需求,要求不管到那个界面,我必须让一个浮动条(其实是个窗体)置顶. 我用wpf,因为有之前有好几个界面已经设置成topmost了,所以在这几个界 ...

  4. Expression Design与Blend制作滚动的小球动画教程

    原文:Expression Design与Blend制作滚动的小球动画教程 一,开发工具 Microsoft Expression Design & Blend 4.0 (3.0亦可). 这两 ...

  5. WPF 获取鼠标屏幕位置、窗口位置、控件位置

    原文:WPF 获取鼠标屏幕位置.窗口位置.控件位置 public struct POINT { public int X; public int Y; public POINT(int x, int ...

  6. ZOJ 3726 RMQ + 二分法

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072 区域赛真干的话题 通过率最高的一个问题 不到一半认为这OK  然后WA果 ...

  7. blockchain_eth客户端安装 & geth使用 &批量转账(一)

    这里是第一篇,主要讲eth客户端安装 eth官网 https://ethereum.org/ 国内有一个论坛内容挺多的,可以参考  http://ethfans.org/ eth客户端: eth客户端 ...

  8. XF 主从页面

    using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation (XamlCompila ...

  9. WPF 控件的内容属性

    WPF的内容属性不应定都是content, 例如TextBlock的内容属性是Text Panel的内容属性是Children ListBox的内容属性是Items

  10. WPF ScrollViewer(滚动条) 自定义样式表制作 图文并茂

    原文:WPF ScrollViewer(滚动条) 自定义样式表制作 图文并茂 先上效果图 正常样式 拖动时样式 好下面 开始吧 ==================================== ...