数据库

 首页 > 数据库 > MongoDB > MongoDB使用Update更新记录

MongoDB使用Update更新记录

分享到:
【字体:
导读:
         摘要:同上,紧接着学习MongoDB的数据更新语法。一.更新一条记录--1.1查询id1的documentsdb.test_2.find({id:1});{_id:ObjectId(50a0d46ceb825d827b0c3f9b),id:1,name:francs}{_id:ObjectId(50a12e34f73f2e4...

MongoDB使用Update更新记录


      同上,紧接着学习 MongoDB 的数据更新语法。



一.更新一条记录
--1.1 查询 id=1 的 documents


 > db.test_2.find({id:1});
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9b"), "id" : 1, "name" : "francs" }
{ "_id" : ObjectId("50a12e34f73f2e4aa1ff71bc"), "id" : 1, "name" : "francs" }

--1.2 更新 id=1 的 documents 的 name 字段为 'name_2'

 > db.test_2.update({id:1},{$set:{name:'name_2'}});


--1.3 再次查询 id=1 的 documents


 > db.test_2.find({id:1});
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9b"), "id" : 1, "name" : "name_2" }
{ "_id" : ObjectId("50a12e34f73f2e4aa1ff71bc"), "id" : 1, "name" : "francs" }
       备注:发现只更新一条匹配的记录。

二. 更新多条记录
--2.1 查询 id=2 的 documents

 > db.test_2.find({id:2});
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9c"), "id" : 2, "name" : "fpZhou" }
{ "_id" : ObjectId("50a12e7cf73f2e4aa1ff71bd"), "id" : 2, "name" : "fpZhou" }
{ "_id" : ObjectId("50a12f47f73f2e4aa1ff71be"), "id" : 2, "name" : "fpZhou" }


--2.2 更新 id=2 的 documents 的 name 字段为 'name_3'


 > db.test_2.update({id:2},{$set:{name:'name_3'}},{multi:true});


--2.3 再次查询 id=2 的 documents


 > ;db.test_2.find({id:2});
{ "_id" : ObjectId("50a0d46ceb825d827b0c3f9c"), "id" : 2, "name" : "name_3" }
{ "_id" : ObjectId("50a12e7cf73f2e4aa1ff71bd"), "id" : 2, "name" : "name_3" }
{ "_id" : ObjectId("50a12f47f73f2e4aa1ff71be"), "id" : 2, "name" : "name_3" }

备注:默认情况下 update() 仅更新第一条匹配的记录,如果想更新多条,需要设置
           multi 参数为 true。


三. 被更新的记录不存在的情况
--3.1 查询 id=6 的 documents


 > db.test_2.find({id:6});

   备注:id=6 的记录不存在。

--3.2 不加 upsert 参数的更新

 > db.test_2.update({id:6},{$set:{name:'name_6'}});
> db.test_2.find({id:6});
>

  备注:这时更新后,没有新增 id=6 的记录。

--3.3 带  upsert 参数的更新

 > db.test_2.update({id:6},{$set:{name:'name_6'}},{upsert:true});
> db.test_2.find({id:6});
{ "_id" : ObjectId("50a1328f7543857379c2bb38"), "id" : 6, "name" : "name_6" }
>

  备注:如果被更新的 document 不存在,可以通过指定 upsert 参数确定是否要插入
一条新记录,如果为 truce 则插入,否则,不插入。


四. 附
附一 .update() 语法
db.collection.update( criteria, objNew, upsert, multi )


criteria - query which selects the record to update;
objNew -   updated object or $ operators (e.g., $inc) which manipulate the
object
upsert -   if this should be an "upsert" operation; that is, if the record(s) do
           not exist, insert one. Upsert only inserts a single document.
multi -    indicates if all documents matching criteria should be updated rather
           than just one. Can be useful with the $ operators below.

     If you are coming from SQL, be aware that by default, update() only modifies
   the first  matched object. If you want to modify all matched objects, you need
   to use the multi flag.


附二. $set 语法
    Use the $set operator to set a particular value. The $set operator requires the
 following syntax:
  db.collection.update( { field: value1 }, { $set: { field1: value2 } } );
 
   This statement updates in the document in collection where field matches value1 by
replacing the value of the field field1 with value2. This operator will add the speci
fied field or fields if they do not exist in this document or replace the existing
valueof the specified field(s) if they already exist.


MongoDB使用Update更新记录
分享到:
Spring与MongoDB实现聚合查询
Spring与MongoDB实现聚合查询 MongoDB和Spring整合,根据开发文档找到对应的版本. 根据官方文档知道Spring必须是3.0.x或以上的版本,MongoDB必须是1.6.5以上的版本才行。 要想整合Spring和Mongodb必须下载相应的jar,这里主要是用到两种jar一种是spring-data-document和spring- data-commons两种类型的jar但是两种类型...
MongoDB数据库的update使用方法详解
MongoDB数据库的update使用方法详解 在前面的文章“mongodb 查询的语法”里,我介绍了Mongodb的常用查询语法,Mongodb的update操作也有点复杂,我结合自己的使用经验,在这里介绍一下,给用mongodb的朋友看看,也方便以后自己用到的时候查阅: 注:在这篇文章及上篇文章内讲的语法介绍都是在mongodb shell环...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……