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类来进行详 ...
随机推荐
- Matlab Tricks(十五) —— 圆的正确画法
使用参数方程, phi = 0:0.01:2*pi; x = cos(phi); y = sin(phi); axis equal plot(x, y) 根据参数方程,显然,圆心在 (0, 0),半径 ...
- 动态加载Layout
因为现在手头上做的需要显示很多不同布局,想着拆分开来不要全部都写到main.xml里,于是就想到动态加载Layout 目前试了下, LinearLayout page = (LinearLayout) ...
- WPF中取得预定义颜色
原文:WPF中取得预定义颜色 使用XAML代码取得.net预定义颜色:<Page xmlns="http://schemas.microsoft.com/winfx/2006/x ...
- poj1548Robots dfs实践
//搜索每一行 //该生产线的整点已被清除 //然后位置,然后转移到下一个步走的最后一点 //然后,所有点的下面一行清晰 //然后重复上面的操作 #include<iostream> #i ...
- 合并ResourceDictionary
原文:合并ResourceDictionary <!--合并资源到资源集合中--> <ResourceDictionary> <ResourceDictionary.Me ...
- windows server疑难杂症
1.某些网址.服务访问失败,可能的原因:增强的安全配置关闭增强的安全配置,并且重启电脑!!!http://jingyan.baidu.com/article/6181c3e076ac0b152ff15 ...
- QT Linux Demo程序编译
我手上的qt源码包为:qt-everywhere-opensource-src-4.7.0.tar.gz 在Linux下编译比较容易,解压后直接 ./configure,一般会报缺少什么库这些.自己遇 ...
- jquery动态创建小广告
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 使用checkpoint做代理服务器
version:R80.30 T200 Step 1:编辑刀片的属性,开启http/https代理,如下图: Step 2:配置访问策略,如下图: Step 3:配置客户端浏览器如下图: Step 4 ...
- android x86 7.0 32bit调试apk时出现的错误
detected problems with app native libraries libavcodec.so:text relocationslibavutil.solibswresample. ...