扫码一下
查看教程更方便
mongodb 的update()和save()方法用于更新集合里的文档内容。update() 方法更新现有文档中的值。save() 方法用传递给它的文档替换现有文档。
update() 方法更新现有文档中的值。
update()方法的基本语法如下
>db.collection_name.update(selection_criteria, updated_data)
考虑 mycol 集合具有以下数据。
{ "_id" : objectid(5983548781331adf45ec5), "title":"mongodb overview"}
{ "_id" : objectid(5983548781331adf45ec6), "title":"nosql overview"}
{ "_id" : objectid(5983548781331adf45ec7), "title":"jiyik overview"}
以下示例将标题为“mongodb overview”的文档的设置为新标题“new mongodb tutorial”。
>db.mycol.update({'title':'mongodb overview'},{$set:{'title':'new mongodb tutorial'}})
writeresult({ "nmatched" : 1, "nupserted" : 0, "nmodified" : 1 })
>db.mycol.find()
{ "_id" : objectid(5983548781331adf45ec5), "title":"new mongodb tutorial"}
{ "_id" : objectid(5983548781331adf45ec6), "title":"nosql overview"}
{ "_id" : objectid(5983548781331adf45ec7), "title":"jiyik overview"}
>
默认情况下,mongodb 将仅更新单个文档。要更新多个文档,需要将参数 'multi' 设置为 true。
>db.mycol.update({'title':'mongodb overview'},
{$set:{'title':'new mongodb tutorial'}},{multi:true})
save() 方法用传递给它的文档替换现有文档。
mongodb save()方法的基本语法如下所示
>db.collection_name.save({_id:objectid(),new_data})
以下示例将使用 _id '5983548781331adf45ec5' 替换文档。
>db.mycol.save(
{
"_id" : objectid("507f191e810c19729de860ea"),
"title":"jiyik new topic",
"by":"jiyik"
}
)
writeresult({
"nmatched" : 0,
"nupserted" : 1,
"nmodified" : 0,
"_id" : objectid("507f191e810c19729de860ea")
})
>db.mycol.find()
{ "_id" : objectid("507f191e810c19729de860e6"), "title":"jiyik new topic",
"by":"jiyik"}
{ "_id" : objectid("507f191e810c19729de860e6"), "title":"nosql overview"}
{ "_id" : objectid("507f191e810c19729de860e6"), "title":"tutorials point overview"}
>
findoneandupdate() 方法首先查处指定条件的文档,然后使用指定的文档更新查出来的文档的值。
findoneandupdate()方法的基本语法如下
>db.collection_name.findoneandupdate(selectioin_criteria, updated_data)
假设我们创建了一个名为 empdetails 的集合并在其中插入了三个文档,如下所示
> db.empdetails.insertmany(
[
{
first_name: "radhika",
last_name: "sharma",
age: "26",
e_mail: "radhika_sharma.123@gmail.com",
phone: "9000012345"
},
{
first_name: "rachel",
last_name: "christopher",
age: "27",
e_mail: "rachel_christopher.123@gmail.com",
phone: "9000054321"
},
{
first_name: "fathima",
last_name: "sheik",
age: "24",
e_mail: "fathima_sheik.123@gmail.com",
phone: "9000054321"
}
]
)
以下示例更新名为“radhika”的文档的年龄和电子邮件值。
> db.empdetails.findoneandupdate(
{first_name: 'radhika'},
{ $set: { age: '30',e_mail: 'radhika_newemail@gmail.com'}}
)
{
"_id" : objectid("5dd6636870fb13eec3963bf5"),
"first_name" : "radhika",
"last_name" : "sharma",
"age" : "30",
"e_mail" : "radhika_newemail@gmail.com",
"phone" : "9000012345"
}
此方法更新与给定过滤器匹配的单个文档。
updateone() 方法的基本语法如下
>db.collection_name.updateone(, )
> db.empdetails.updateone(
{first_name: 'radhika'},
{ $set: { age: '30',e_mail: 'radhika_newemail@gmail.com'}}
)
{ "acknowledged" : true, "matchedcount" : 1, "modifiedcount" : 0 }
>
updatemany() 方法更新与给定过滤器匹配的所有文档。
updatemany() 方法的基本语法如下
>db.collection_name.update(, )
> db.empdetails.updatemany(
{age:{ $gt: "25" }},
{ $set: { age: '00'}}
)
{ "acknowledged" : true, "matchedcount" : 2, "modifiedcount" : 2 }
如果您使用 find 方法检索文档的内容,可以看到更新的值,如下所示 -
> db.empdetails.find()
{ "_id" : objectid("5dd6636870fb13eec3963bf5"), "first_name" : "radhika", "last_name" : "sharma", "age" : "00", "e_mail" : "radhika_newemail@gmail.com", "phone" : "9000012345" }
{ "_id" : objectid("5dd6636870fb13eec3963bf6"), "first_name" : "rachel", "last_name" : "christopher", "age" : "00", "e_mail" : "rachel_christopher.123@gmail.com", "phone" : "9000054321" }
{ "_id" : objectid("5dd6636870fb13eec3963bf7"), "first_name" : "fathima", "last_name" : "sheik", "age" : "24", "e_mail" : "fathima_sheik.123@gmail.com", "phone" : "9000054321" }
>