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类来进行详 ...
随机推荐
- C#7
C#7 阅读目录 out变量 元组(Tuples) 模式匹配(Pattern matching) 本地引用和返回(Ref locals and returns) 本地函数(Local function ...
- yii联查
$count = Acticle::find()->select("acticle_type.act_type,acticle.act_id,acticle.act_title,act ...
- Android 悬浮窗、悬浮球开发
原文:Android 悬浮窗.悬浮球开发 1.权限管理 直接看我另外一篇博客吧,传送门: https://my.oschina.net/u/1462828/blog/1933162 2.Base类Ba ...
- windows 的使用 —— 注册表(软件的安装和卸载)
win + r(run):输入 regedit(register edit)进入: 1. 网络连接 比如一些 vpn 安装之后,会对网络连接进行一定的修改,这样在 vpn 工具删除之后,仍然无法消除修 ...
- 离散时间信号常见函数的实现(matlab)
1. 单位样本序列 δ(n−n0)={1,n=n00,n≠n0 function [x, n] = impseq(n0, n1, n2) n = n1:n2; x = [n == n0]; 2. 单位 ...
- QT实现鼠标钩子(使用SetWindowsHookEx安装mouseProc函数)
HHOOK mouseHook=NULL; LRESULT CALLBACK mouseProc(int nCode,WPARAM wParam,LPARAM lParam ) { if(nCode ...
- jquery 鼠标经过延时触发事件,jquery插件
jquery 鼠标经过延时触发事件. 用来做:鼠标经过选项卡,延时切换,鼠标经过商城分类延时显示,最好不过,防止用户随便滑动就切换了选项卡. 一.引入jq 二.加入以下插件代码 /* * 鼠标经过延时 ...
- android adb socket 通信
今天遇到一个问题:pc客户端和android的App通信,心跳通道(心跳包27个字节,是一个业务空包)在部分pc上总是会超时(5秒超时),nagle算法也给禁用了,pc端时按按量发送心跳的,怀疑来怀疑 ...
- Delphi中流对象的应用
Delphi的流对象(TStream的派生对象)有如下读写函数: function Read(var Buffer; Count: Longint): Longint;function Write(c ...
- NAudio的使用说明
官方网站:http://naudio.codeplex.com/ 源码:https://github.com/naudio/NAudio NuGet安装: Install-Package NAudio ...