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" ] }

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

mongodb 批量更新 数组的键操作的文件的更多相关文章

  1. mongodb批量更新操作文档的数组键

    persons文档的数据如下: > db.persons.find(){ "_id" : 2, "name" : 2 }{ "_id" ...

  2. MongoDB批量更新和批量插入的方式

    最近,在调试代码中发现向MongoDB插入或者更新文档记录时若是多条的话都是采用for循环操作的,这样的处理方式会造成数据操作耗时,不符合批量处理的原则:对此,个人整理了一下有关MongoDB的批量更 ...

  3. MongoDB创建\更新\删除文档操作

     一.插入\创建文档 --当插入一个不存在的文档时,会自己主动创建一个文档 [root@racdb ~]# mongo MongoDB shell version: 2.4.14 connecti ...

  4. mongodb批量更新某个字段

    查询出hospitalName是xx医院和openId以2开头的所有记录,并且更新my_booking表中的payType为1. db.getCollection('my_booking').find ...

  5. c# Mongodb批量更新

    public void Put(List<OnlineItem> datas)         {             try             {                ...

  6. Mongodb 批量更新

    >db.col.update({查询条件},{修改条件},{multi:true})

  7. C# .NET Core 3.1中使用 MongoDB.Driver 更新嵌套数组元素和关联的一些坑

    C# .NET Core 3.1中使用 MongoDB.Driver 更新数组元素和关联的一些坑 前言: 由于工作的原因,使用的数据库由原来的 关系型数据库 MySQL.SQL Server 变成了 ...

  8. MongDB 批量更新

    最近公司在使用mongodb.  批量更新的语句为: db.table.update(  {'wo': {$in: [ "123", "456"]}}, {$s ...

  9. MongoDB学习笔记~大叔分享批量添加—批量更新—批量删除

    回到目录 说它是批量操作,就是说将集合对象一次提交到服务器,并对数据进行持久化,如果您的代码是一次一次的提交,那不算是批量操作!在之前的mongodb仓储中并没有对批量更新和批量删除进行实现,而今天在 ...

随机推荐

  1. [NodeJS] Use Secrets When Deploying Applications with Now

    Applications require a lot of sensitive information. Database passwords, API keys and secrets used f ...

  2. Redis笔记---set

    1.redis set的介绍 集合中的数据是不重复且没有顺序,集合类型和列表类型的对比. 集合类型:存储的是的是最多2的32次方减一个字符串,数据是没有顺序的,但是数据是唯一的 列表类型:最多存储内容 ...

  3. LA 3135 - Argus

    看题:传送门 大意就是让你编写一个称为argus的系统,这个系统支持一个register的命令: Register  Q_num Period 该命令注册了一个触发器,它每Period秒就会残生一个编 ...

  4. wireshark分析包中关于三次握手和四次终止标识

    转自: http://hi.baidu.com/hepeng597/item/5ba27e0b98bc8de3ff240de0 三次握手Three-way Handshake 一个虚拟连接的建立是通过 ...

  5. stm32的adc时钟周期,ADC_SampleTime_1Cycles5是多长时间

  6. Java RMI使用

    1. Java RMI介绍 RMI:远程方法调用(Remote Method Invocation).能够让在某个java虚拟机上的对象像调用本地对象方法一样调用另一个java 虚拟机中的对象上的方法 ...

  7. LA 3942 - Remember the Word 字典树+DP

    看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  8. 23种设计模式——Prototype模式

    Prototype模式是提供自我复制的功能.包括浅拷贝和深拷贝. 一.Prototype模式的用途 场景1:游戏场景中有很多类似的敌人,它们的技能都一样,但是随着敌人出现的位置和不同,它们的能力也不太 ...

  9. 用Eclipse替代Keil&IAR来开发ARM应用(升级版)

    Eclipse GNU ARM Plugin 2014/7/16 作者 kiya 几个月前写了一篇<),想自己丰衣足食的参考我的上一篇文章,以及GNU ARM的官网. 用Eclipse替代Kei ...

  10. mysql数据库 navicat premium mac 破解教程

    https://www.jianshu.com/p/f3ef78deadaa 转自Navicat Premium for Mac v12.0.22.0 破解教程,macOS上手动破解,无需补丁,无毒 ...