码迷,mamicode.com
首页 > 数据库 > 详细

mogodbshell中数组对象查询修改方法

时间:2017-09-06 10:59:31      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:记录   style   cti   obj   roman   需求   标题   查询   pre   

在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 (中国标准时间)" }

核心是$,可以理解为数组定位器

83334129

mogodbshell中数组对象查询修改方法

标签:记录   style   cti   obj   roman   需求   标题   查询   pre   

原文地址:http://www.cnblogs.com/qkabcd/p/7483357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!