`
rfe48rfe
  • 浏览: 9113 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

mongodb update

 
阅读更多

mongodb update
2011年05月13日
  mongodb更新有两个命令:
  1).update()命令
  db.collection.update( criteria, objNew, upsert, multi )
  criteria : update的查询条件,类似sql update查询内where后面的
  objNew   : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
  upsert   : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
  multi    : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
  例:
  db.test0.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一条记录
  db.test0.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 全更新了
  db.test0.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 只加进去了第一条
  db.test0.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 全加进去了
  db.test0.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );全更新了
  db.test0.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );只更新了第一条
  2).save()命令
  db.collection.save( x )
  x就是要更新的对象,只能是单条记录。
  如果在collection内已经存在一个和x对象相同的"_id"的记录。mongodb就会把x对象替换collection内已经存在的记录,否则将会插入x对象,如果x内没有_id,系统会自动生成一个再插入。相当于上面update语句的upsert=true,multi=false的情况。
  例:
  db.test0.save({count:40,test1:"OK"}); #_id系统会生成
  db.test0.save({_id:40,count:40,test1:"OK"}); #如果test0内有_id等于40的,会替换,否则插入。
  mongodb的更新操作符:
  1) $inc
  用法:{ $inc : { field : value } }
  意思对一个数字字段field增加value,例:
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 16, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $inc : { "count" : 1 } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 17, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $inc : { "count" : 2 } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 19, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $inc : { "count" : -1 } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }
  2) $set
  用法:{ $set : { field : value } }
  就是相当于sql的set field = value,全部数据类型都支持$set。例:
  > db.test0.update( { "_id" : 15 } , { $set : { "test1" : "testv1","test2" : "testv2","test3" : "testv3","test4" : "testv4" } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : "testv1", "test2" : "testv2", "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }
  3) $unset
  用法:{ $unset : { field : 1} }
  顾名思义,就是删除字段了。例:
  > db.test0.update( { "_id" : 15 } , { $unset : { "test1":1 } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test2" : "testv2", "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $unset : { "test2": 0 } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $unset : { "test3":asdfasf } } );
  Fri May 14 16:17:38 JS Error: ReferenceError: asdfasf is not defined (shell):0
  > db.test0.update( { "_id" : 15 } , { $unset : { "test3":"test" } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test4" : "testv4", "test5" : "OK" }
  没看出field : 1里面的1是干什么用的,反正只要有东西就行。
  4) $push
  用法:{ $push : { field : value } }
  把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去。例:
  > db.test0.update( { "_id" : 15 } , { $set : { "test1" : ["aaa","bbb"] } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb" ], "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $push : { "test1": "ccc" } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc" ], "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $push : { "test2": "ccc" } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc" ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $push : { "test1": ["ddd","eee"] } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc", [ "ddd", "eee" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }5) $pushAll
  用法:{ $pushAll : { field : value_array } }
  同$push,只是一次可以追加多个值到一个数组字段内。例:
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc", [ "ddd", "eee" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $pushAll : { "test1": ["fff","ggg"] } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc", [ "ddd", "eee" ], "fff", "ggg" ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
  6) $addToSet
  用法:{ $addToSet : { field : value } }
  增加一个值到数组内,而且只有当这个值不在数组内才增加。例:
  > db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": {$each : ["444","555"] } } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
  "aaa",
  "bbb",
  "ccc",
  [
  "ddd",
  "eee"
  ],
  "fff",
  "ggg",
  [
  "111",
  "222"
  ],
  "444",
  "555"
  ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": {$each : ["444","555"] } } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
  "aaa",
  "bbb",
  "ccc",
  [
  "ddd",
  "eee"
  ],
  "fff",
  "ggg",
  [
  "111",
  "222"
  ],
  "444",
  "555"
  ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": ["444","555"] } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
  "aaa",
  "bbb",
  "ccc",
  [
  "ddd",
  "eee"
  ],
  "fff",
  "ggg",
  [
  "111",
  "222"
  ],
  "444",
  "555",
  [
  "444",
  "555"
  ]
  ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": ["444","555"] } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
  "aaa",
  "bbb",
  "ccc",
  [
  "ddd",
  "eee"
  ],
  "fff",
  "ggg",
  [
  "111",
  "222"
  ],
  "444",
  "555",
  [
  "444",
  "555"
  ]
  ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
  7) $pop
  删除数组内的一个值
  用法:
  删除最后一个值:{ $pop : { field : 1 } }删除第一个值:{ $pop : { field : -1 } }
  注意,只能删除一个值,也就是说只能用1或-1,而不能用2或-2来删除两条。mongodb 1.1及以后的版本才可以用,例:
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
  "bbb",
  "ccc",
  [
  "ddd",
  "eee"
  ],
  "fff",
  "ggg",
  [
  "111",
  "222"
  ],
  "444"
  ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $pop : { "test1": -1 } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
  "ccc",
  [
  "ddd",
  "eee"
  ],
  "fff",
  "ggg",
  [
  "111",
  "222"
  ],
  "444"
  ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $pop : { "test1": 1 } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", "ggg", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4",
  "test5" : "OK" }
  8) $pull
  用法:$pull : { field : value } }
  从数组field内删除一个等于value值。例:
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", "ggg", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4",
  "test5" : "OK" }
  > db.test0.update( { "_id" : 15 } , { $pull : { "test1": "ggg" } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5"
  : "OK" }
  9) $pullAll
  用法:{ $pullAll : { field : value_array } }
  同$pull,可以一次删除数组内的多个值。例:
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5"
  : "OK" }
  > db.test0.update( { "_id" : 15 } , { $pullAll : { "test1": [ "ccc" , "fff" ] } } );
  > db.test0.find( { "_id" : 15 } );
  { "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ [ "ddd", "eee" ], [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
  10) $ 操作符
  $是他自己的意思,代表按条件找出的数组里面某项他自己。呵呵,比较坳口。看一下官方的例子:
  > t.find()
  { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }
  > t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true )
  > t.find()
  { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" : 7 } ] }
  需要注意的是,$只会应用找到的第一条数组项,后面的就不管了。还是看例子:
  > t.find();
  { "_id" : ObjectId("4b9e4a1fc583fa1c76198319"), "x" : [ 1, 2, 3, 2 ] }
  > t.update({x: 2}, {$inc: {"x.$": 1}}, false, true);
  > t.find();
  还有注意的是$配合$unset使用的时候,会留下一个null的数组项,不过可以用{$pull:{x:null}}删除全部是null的数组项。例:
  > t.insert({x: [1,2,3,4,3,2,3,4]})
  > t.find()
  { "_id" : ObjectId("4bde2ad3755d00000000710e"), "x" : [ 1, 2, 3, 4, 3, 2, 3, 4 ] }
  > t.update({x:3}, {$unset:{"x.$":1}})
  > t.find()
  { "_id" : ObjectId("4bde2ad3755d00000000710e"), "x" : [ 1, 2, null, 4, 3, 2, 3, 4 ] }
  { "_id" : ObjectId("4b9e4a1fc583fa1c76198319"), "x" : [ 1, 3, 3, 2 ] }
  http://hi.baidu.com/ixigua/blog/item/4d0ce436a4a7d 63e0b55a914.html
分享到:
评论

相关推荐

    mongodb update操作符ppt

    主要内容详见 https://blog.csdn.net/yishengzhiaiyige/article/details/123542233

    Spring Data MongoDB : Update document

    NULL 博文链接:https://study121007.iteye.com/blog/2307592

    MongoDB中的加减乘除运算详解

    很多同学因为对MongoDB不熟悉,加之应用的不是很多,有时候会认为MongoDB数据库对一些功能不支持,或者认为支持不好。今天我们 演示一下 MongoDB对“加减乘除”的使用。 在MongoDB数据库中“加减乘除”运算,又称为 ...

    update-mongo:在Node中运行MongoDB更新Stript的简单方法

    update-mongo允许您运行MongoDB的更新脚本,而无需与进行交互。 这是MIT许可下的一个开源项目,有关更多信息,请参见 。 跳到有关如何使用update-mongo 安装 npm install --save update-mongo 用法 提供了一种...

    如何在C#中为MongoDB建立更新查询

    请查看更新现有文档的信息[^]。也请尝试http://docs.mongodb.org/ecosystem/tutorial/use-csharp-driver/[^]。

    Can't find a codec for class java.math.BigDecimal.txt

    解决mongo数据插入时 报错问题 mogodb插入数据时报错Can't find a codec for class java.math.BigDecimal

    MongoDB基本操作指南

    CRUD更加简单,支持in-place update:只要定义一个数组,然后传递给MongoDB的insert/update方法就可自动插入或更新;对于更新模式,MongoDB支持一个upsert选项,即:“如果记录存在那么更新,否则插入”。MongoDB的...

    mongodb3.2.8入门到精通视频教程.zip

    目录网盘文件永久链接 1.准备工作之下载centos,mongodb,GUI安装和介绍 2.Databases和Collections,Document概念分析 3.Capped Collections介绍及生产环境之疲劳度过滤,...11.mongodb之update运算符分析二...............

    mongodb常用函数使用案例

    mongodb常用函数使用案例,主要是针对update,insert,find函数

    mongodb-win32-x86_64-2008plus-ssl-v3.4-latest-signed.msi

    MongoDb 使用update()命令可以实现替换完成的文档(数据)或者一些指定的数据字段 。 Mongodb中的Map/reduce主要是用来对数据进行批量处理和聚合操作。 Map和Reduce。Map函数调用emit(key,value)遍历集合中所有的...

    MongoDB 更新文档

    MongoDB 使用 update() 和 save() 方法来更新集合中的文档。接下来让我们详细来看下两个函数的应用及其区别。 update() 方法 update() 方法用于更新已存在的文档。语法格式如下: db.collection.update( , , { ...

    mongodb资料大全

    收集整理的mongodb学习资料,与大家分享。 包括: MongoDB 概念理解.pdf MongoDB_使用手册-中文版.pdf MongoDB使用手册.pdf Mongodb文档 与 php操作.pdf MongoDB应用.pdf =========================================...

    MongoDB增删改查工具类

    MongoDB增删改查工具类,根据一个doc,来检索,当doc是空的时候检索全部 检索全部并返回迭代器 便利迭代器FindIterable /** * Dao 层接口 * */ public interface MongoDao { /** * Get Data BY ID * * @...

    PHP如何连接mongo,PHP连接mongodb

    PHP如何连接mongo,PHP连接mongodb的方法,和查询,update、insert、find

    Ubuntu18.04 安装MongoDB 创建用户及远程访问

    Ubuntu 安装MongoDB 1.在终端输入GPK码 2.添加mongoDB源 . echo "deb [ arch=amd64,arm64 ] ...3.sudo apt update 3.安装MongoDB sudo apt-get install -y mongodb-org 4.安装完成执行sudo service mongo

    mongodb使用-增删改查、colleciton关联、_id作为主键实现update

    这篇主要是讲下mongodb数据库的操作。web前端通过ajax把数据按照yang的格式传给servlet,然后servlet再给相应的url传输,就是对应的yang文件的某个rpc,rpc收到后进行解析。

    基于Java的ORM框架Mongodb-ORM.zip

    mongodb-orm简介Mongodb ORM是基于java的ORM框架,简化了SDK的使用,使代码变得更清晰、简单。 与Ibatis类似,将查询、执行语句封装在xml中,与代码隔离。简称MQL。 项目中使用加入mongodb orm的支持包1. 添加jar...

    JavaScript Applications with Node.js, React, React Native and MongoDB

    JavaScript Applications with Node.js, React, React Native and MongoDB: Design, code, test, deploy and manage in Amazon AWS By 作者: Eric Bush ISBN-10 书号: 0997196661 ISBN-13 书号: 9780997196665 出版...

    mongoDB基础查询/更新语句笔记

    实用有效,最基础的查询显示字段、排序、分页、in 、范围查询和更新字段语句

    mongodb数据库基本操作.pdf

    mongodb 数据库基本...db.mycollection.updateOne({key: "value"}, {$set: {newkey: "newvalue"}}) ``` ### 删除文档 ```bash db.mycollection.deleteOne({key: "value"}) ``` ### 创建索引 ```bash db.mycollecti

Global site tag (gtag.js) - Google Analytics