博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongodb 批量更新 数组的键操作的文件
阅读量:5058 次
发布时间:2019-06-12

本文共 6909 字,大约阅读时间需要 23 分钟。

persons该文件的数据如下面的:

> db.persons.find()

{ "_id" : 2, "name" : 2 }
{ "_id" : 3, "name" : 3 }

> db.persons.update({_id:4},{_id:4,name:4})

WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.persons.find()
{ "_id" : 2, "name" : 2 }
{ "_id" : 3, "name" : 3 }

做完update操作,依旧看不到_id:4的记录。由于update方法须要一个true指示器。才会对查询不到的记录进行insert操作:

> db.persons.update({_id:4},{_id:4,name:4},true)

WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 4 })
> db.persons.find()
{ "_id" : 2, "name" : 2 }
{ "_id" : 3, "name" : 3 }
{ "_id" : 4, "name" : 4 }

现有需求。将persons文档中的name为"3"的改成"33"

> db.persons.update({name:"3"},{$set:{name:"33"}},false,true)

false含义:若查不到name:"33"的键值对,则不运行插入操作,true含义:表示是批量更新

为persons添加age:"88"属性

> db.persons.update({name:"4"},{$set:{age:"88"}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4", "age" : "88" }

将age加2

> db.persons.update({name:"4"},{$inc:{age:2}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4", "age" : 90 }

将age属性拿走:

> db.persons.update({age:90},{$unset:{age:1}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }

给persons文档添加一条记录,_id为5

> db.persons.insert({_id:5,name:5,books:[]})

WriteResult({ "nInserted" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "name" : 5, "books" : [ ] }

给books数组添加一个元素:"js"和"extjs4.0"

> db.persons.update({_id:5},{$push:{books:"js"}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "name" : 5, "books" : [ "js" ] }

> db.persons.update({_id:5},{$push:{books:"extjs4.0"}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "name" : 5, "books" : [ "js", "extjs4.0" ] }

创建一个新的classes数组:

> db.persons.update({_id:5},{$push:{classes:"01class"}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "name" : 5, "books" : [ "js", "extjs4.0" ], "classes" : [ "01class"
 ] }

为calsses数组一次添加几个元素:

> db.persons.update({_id:5},{$pushAll:{classes:["02class","03class","04class"]}}

)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "name" : 5, "books" : [ "js", "extjs4.0" ], "classes" : [ "01class"
, "02class", "03class", "04class" ] }

删除_id是5的记录,并创建一个新的_id是5的记录,使用$addToSet,此命令会检查要加入的元素在数组里面是不是存在,存在就不会再加入。否则会加入:

> db.persons.remove({_id:5})

WriteResult({ "nRemoved" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
> db.persons.insert({_id:5,books:["js"]})
WriteResult({ "nInserted" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "js" ] }
> db.persons.update({_id:5},{$addToSet:{books:"js"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "js" ] }
> db.persons.update({_id:5},{$addToSet:{books:"java"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "js", "java" ] }
> db.persons.update({_id:5},{$addToSet:{books:"mongo"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "js", "java", "mongo" ] }

删除books数组的第一个元素:"js"。使用$pop命令:

> db.persons.update({_id:5},{$pop:{books:-1}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "java", "mongo" ] }
> db.persons.update({_id:5},{$pop:{books:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "java" ] }

-1代表第一个元素,1代表最后一个元素

也能够使用pull命令一次删除一个指定的元素:

> db.persons.find()

{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "java", "mongo", "js" ] }
> db.persons.update({_id:5},{$pull:{books:"js"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "java", "mongo" ] }

$pullAll命令能够一次指定多个要删除的元素:

> db.persons.update({_id:5},{$pullAll:{books:["java","mongo"]}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ ] }

创建一条新的记录_id为6:

> db.persons.find()

{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ ] }
{ "_id" : 6, "books" : [ { "type" : "js", "name" : "extjs4.0" }, { "type" : "db"
, "name" : "mongodb" }, { "type" : "js", "name" : "jquery" } ] }

为type是js的books元素加入pens:"too long"属性。使用.符号一定使用双引號引用

> db.persons.update({"books.type":"js"},{$set:{"books.$.author":"tom"}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ ] }
{ "_id" : 6, "books" : [ { "type" : "js", "name" : "extjs4.0", "author" : "tom"
}, { "type" : "db", "name" : "mongodb" }]
 }

 db.persons.update({"books.type":"js"},{$set:{"books.$.pens":"too long"}})

推断数组元素运行插入操作,反复的元素不会被插入第二次:

> db.persons.find()

{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4 }
{ "_id" : 5, "books" : [ "js" ] }

> db.persons.update({_id:5},{$addToSet:{books:{$each:["js","db","java"]}}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4 }
{ "_id" : 5, "books" : [ "js", "db", "java" ] }

版权声明:本文博主原创文章,博客,未经同意不得转载。

转载于:https://www.cnblogs.com/gcczhongduan/p/4853029.html

你可能感兴趣的文章
Java8 Lambda表达应用 -- 单线程游戏server+异步数据库操作
查看>>
[Unity3D]Unity3D游戏开发MatchTarget的作用攀登效果实现
查看>>
AngularJS学习篇(一)
查看>>
关于Xshell无法连接centos6.4的问题
查看>>
css3动画——基本准则
查看>>
输入月份和日期,得出是今年第几天
查看>>
pig自定义UDF
查看>>
Kubernetes 运维学习笔记
查看>>
spring security 11种过滤器介绍
查看>>
代码实现导航栏分割线
查看>>
一个mysql主从复制的配置案例
查看>>
大数据学习系列(8)-- WordCount+Block+Split+Shuffle+Map+Reduce技术详解
查看>>
【AS3代码】播放FLV视频流的三步骤!
查看>>
枚举的使用
查看>>
luogu4849 寻找宝藏 (cdq分治+dp)
查看>>
日志框架--(一)基础篇
查看>>
关于源程序到可运行程序的过程
查看>>
转载:mysql数据库密码忘记找回方法
查看>>
scratch少儿编程第一季——06、人在江湖混,没有背景怎么行。
查看>>
【贪心+DFS】D. Field expansion
查看>>