MongoDB数据更新

MongoDB数据更新可以使用update()函数。

db.collection.update( criteria, objNew,upsert, multi )

update()函数接受以下四个参数:

•       criteria : update的查询条件,类似sql update查询内where后面的。

•       objNew : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的

•       upsert : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。

•       multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

>db.blog.insert({"name":"jack","id":88})  --插入记录

WriteResult({ "nInserted" : 1 })

>db.blog.insert({"name":"jack","id":88})  --插入记录

WriteResult({ "nInserted" : 1 })

> db.blog.find()

{ "_id" :ObjectId("5438dd3fa7ccb1d4ecc7571d"), "title" : "myblog post", "conte

nt" : "here is blog past","date" : ISODate("2014-10-11T07:32:55.652Z")}

{ "_id" :ObjectId("5438df80a7ccb1d4ecc7571e"), "author" :"jackwangwei" }

{ "_id" :ObjectId("543b3115a55dc3b8e7c4e932"), "author33" :"jackwangwei" }

{ "_id" :ObjectId("543b35e56f525084e39aab40"), "name" :"jack", "id" : 88 }

{ "_id" :ObjectId("543b35e76f525084e39aab41"), "name" :"jack", "id" : 88 }

> db.blog.update({ "name" :"jack"},{"name":"jack12"}, { "upsert" :true });

WriteResult({ "nMatched" : 1,"nUpserted" : 0, "nModified" : 1 })

> db.blog.find()

{ "_id" :ObjectId("5438dd3fa7ccb1d4ecc7571d"), "title" : "myblog post", "conte

nt" : "here is blog past","date" : ISODate("2014-10-11T07:32:55.652Z")}

{ "_id" :ObjectId("5438df80a7ccb1d4ecc7571e"), "author" :"jackwangwei" }

{ "_id" :ObjectId("543b3115a55dc3b8e7c4e932"), "author33" :"jackwangwei" }

{ "_id" :ObjectId("543b35e56f525084e39aab40"), "name" :"jack12" }   --只更新一条记录,并且id没有了,相当于删除了字段

{ "_id" :ObjectId("543b35e76f525084e39aab41"), "name" :"jack", "id" : 88 }

>  db.blog.update({ "name" :"jack"},{$set:{"name":"jack34"}})  加上$set更新

WriteResult({ "nMatched" : 1,"nUpserted" : 0, "nModified" : 1 })

> db.blog.find()

{ "_id" :ObjectId("5438dd3fa7ccb1d4ecc7571d"), "title" : "myblog post", "conte

nt" : "here is blog past","date" : ISODate("2014-10-11T07:32:55.652Z")}

{ "_id" :ObjectId("5438df80a7ccb1d4ecc7571e"), "author" :"jackwangwei" }

{ "_id" : ObjectId("543b3115a55dc3b8e7c4e932"),"author33" : "jackwangwei" }

{ "_id" :ObjectId("543b35e56f525084e39aab40"), "name" :"jack12" }

{ "_id" :ObjectId("543b35e76f525084e39aab41"), "name" :"jack34", "id" : 88 }

> db.blog.update({"name" : "jack12"},{$set:{"id":"jack34"}})

WriteResult({ "nMatched" : 1,"nUpserted" : 0, "nModified" : 1 })

> db.blog.find()

{ "_id" :ObjectId("5438dd3fa7ccb1d4ecc7571d"), "title" : "myblog post", "conte

nt" : "here is blog past","date" : ISODate("2014-10-11T07:32:55.652Z")}

{ "_id" :ObjectId("5438df80a7ccb1d4ecc7571e"), "author" :"jackwangwei" }

{ "_id" :ObjectId("543b3115a55dc3b8e7c4e932"), "author33" :"jackwangwei" }

{ "_id" :ObjectId("543b35e56f525084e39aab40"), "name" :"jack12", "id" : "jack3

4" }  --相当于添加了字段并赋值

{ "_id" :ObjectId("543b35e76f525084e39aab41"), "name" :"jack34", "id" : 88 }

 

> db.blog.update({ "name" :"wangwei"},{$set:{"id":"jack34"}},{ "upsert" : true

})   --更新的记录不存在就插入

WriteResult({

       "nMatched" : 0,

       "nUpserted" : 1,

       "nModified" : 0,

       "_id" : ObjectId("543b386ca55dc3b8e7c4e933")

})

> db.blog.find()

{ "_id" :ObjectId("5438dd3fa7ccb1d4ecc7571d"), "title" : "myblog post", "cont

nt" : "here is blog past","date" : ISODate("2014-10-11T07:32:55.652Z")}

{ "_id" : ObjectId("5438df80a7ccb1d4ecc7571e"),"author" : "jackwangwei" }

{ "_id" :ObjectId("543b3115a55dc3b8e7c4e932"), "author33" :"jackwangwei" }

{ "_id" :ObjectId("543b35e56f525084e39aab40"), "name" :"jack12", "id" : "jack

4" }

{ "_id" :ObjectId("543b35e76f525084e39aab41"), "name" :"jack34", "id" : 88 }

{ "_id" :ObjectId("543b386ca55dc3b8e7c4e933"), "name" :"wangwei", "id" : "jac

34" }

> db.blog.update({ "id" :"jack34"},{$set:{"id":"jack"}},{multi:true}) –默认更新,把multi设置为true可以更新多行

WriteResult({ "nMatched" : 2, "nUpserted": 0, "nModified" : 2 })

> db.blog.find()

{ "_id" :ObjectId("5438dd3fa7ccb1d4ecc7571d"), "title" : "myblog post", "conte

nt" : "here is blog past","date" : ISODate("2014-10-11T07:32:55.652Z")}

{ "_id" :ObjectId("5438df80a7ccb1d4ecc7571e"), "author" :"jackwangwei" }

{ "_id" :ObjectId("543b3115a55dc3b8e7c4e932"), "author33" :"jackwangwei" }

{ "_id" :ObjectId("543b35e56f525084e39aab40"), "name" :"jack12", "id" : "jack"

 }

{ "_id" :ObjectId("543b35e76f525084e39aab41"), "name" : "jack34","id" : 88 }

{ "_id" :ObjectId("543b386ca55dc3b8e7c4e933"), "name" :"wangwei", "id" : "jack

" }

 

MongoDB Save() 方法也可以实现更新

save() 方法替换现有的文档和通过新的文档 save() 方法

语法

MongoDB 的 save() 方法的基本语法如下:

>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})

 

> db.blog.save({"_id" :ObjectId("5438df80a7ccb1d4ecc7571e"),"author":"save"})

WriteResult({ "nMatched" : 1,"nUpserted" : 0, "nModified" : 1 })

> db.blog.find()

{ "_id" :ObjectId("5438dd3fa7ccb1d4ecc7571d"), "title" : "myblog post", "conte

nt" : "here is blog past","date" : ISODate("2014-10-11T07:32:55.652Z")}

{ "_id" :ObjectId("5438df80a7ccb1d4ecc7571e"), "author" :"save" }

{ "_id" :ObjectId("543b3115a55dc3b8e7c4e932"), "author33" :"jackwangwei" }

{ "_id" :ObjectId("543b35e56f525084e39aab40"), "name" :"jack12", "id" : "jack"

 }

{ "_id" :ObjectId("543b35e76f525084e39aab41"), "name" :"jack34", "id" : 88 }

{ "_id" :ObjectId("543b386ca55dc3b8e7c4e933"), "name" :"wangwei", "id" : "jack

" }

只更新第一条记录:

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 );

你可能感兴趣的:(mongdb学习)