千家信息网

MongoDB 更新的简单使用

发表于:2025-01-21 作者:千家信息网编辑
千家信息网最后更新 2025年01月21日,update 函数似乎比insert,remove跟find难用一点,原因参数多了。不过简单的写一些比较少用的技巧,普通更新一般比较简单。关于$inc操作:属于原子操作,可以整型,长整型或浮点数进行增
千家信息网最后更新 2025年01月21日MongoDB 更新的简单使用

update 函数似乎比insert,remove跟find难用一点,原因参数多了。

不过简单的写一些比较少用的技巧,普通更新一般比较简单。


关于$inc操作:

属于原子操作,可以整型,长整型或浮点数进行增加操作,如下:

> test={... "name":"Mike",... "friends":32,... "enemies":2,... "Balance":155.6... }{ "name" : "Mike", "friends" : 32, "enemies" : 2, "Balance" : 155.6 }> db.updatetest.insert(test)WriteResult({ "nInserted" : 1 })> db.updatetest.update({"name":"Mike"},{"$inc":{"Balance":5.4}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.updatetest.find(){ "_id" : ObjectId("5625ab9b16a7b138cf6bf46f"), "name" : "Mike", "friends" : 32, "enemies" : 2, "Balance" : 161 }


关于$set操作:

对存在的文档指定一个字段值,如果该字段不存在则创建它,如下:

test={    "name":"Mike",    "age":32,    "sex":"male",    "location":"China"}> db.updatetest.insert(test)WriteResult({ "nInserted" : 1 })> db.updatetest.find(){ "_id" : ObjectId("5625ae7616a7b138cf6bf471"), "name" : "Mike", "age" : 32, "sex" : "male", "location" : "China" }



增加亲属关系:

> relatives={"Wife":"May","Father":"Joe","Mother":"Jojo"}{ "Wife" : "May", "Father" : "Joe", "Mother" : "Jojo" }> db.updatetest.update({"_id":ObjectId("5625ae7616a7b138cf6bf471")}, {"$set":{"relatives":relatives}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.updatetest.find(){ "_id" : ObjectId("5625ae7616a7b138cf6bf471"), "name" : "Mike", "age" : 32, "sex" : "male", "location" : "China", "relatives" : { "Wife" : "May", "Father" : "Joe", "Mother" : "Jojo" } }



利用update操作数组:

$push可以对已有的文档中的数组尾部加入一个元素,如果没有就直接创建一个新数组,如下:

people={    "title":"mail list",    "Phone":"010-123456"}> db.updatetest.insert(people)WriteResult({ "nInserted" : 1 })
db.updatetest.update({"title":"mail list"},  {"$push":{"emails":"test@123.com"}})db.updatetest.update({"title":"mail list"}, {"$push":{"emails":"test@123.com"}})               > db.updatetest.find(){ "_id" : ObjectId("5625ae7616a7b138cf6bf471"), "name" : "Mike", "age" : 32, "sex" : "male", "location" : "China", "relatives" : { "Wife" : "May", "Father" : "Joe", "Mother" : "Jojo" } }{ "_id" : ObjectId("5625b11216a7b138cf6bf472"), "title" : "mail list", "Phone" : "010-123456", "emails" : [ "test@123.com", "test@123.com" ] }


$push是允许重复插入记录的。


$push还允许与slice,sort,each一起使用,如下:

db.updatetest.update({"title":"mail list"}, {"$push":{"emails":{$each:["test1@123.com","test2@124.com","test3@125.com"]}}}) > db.updatetest.find(){ "_id" : ObjectId("5625b2ba16a7b138cf6bf473"), "title" : "mail list", "Phone" : "010-123456", "emails" : [ "test@123.com", "test@123.com", "test1@123.com", "test2@124.com", "test3@125.com" ] }



与$slice一起使用:

> db.updatetest.update({"title":"mail list"},{$push:{"top10":{"$each":["KFC","mcdonalds","pizzahut"],"$slice":-3}}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

没有top10这个键的时候会自动创建一个

> db.updatetest.find(){ "_id" : ObjectId("5625b44d16a7b138cf6bf474"), "title" : "mail list", "Phone" : "010-123456", "emails" : [ "test1@123.com", "test2@124.com", "test3@125.com" ], "top10" : [ "KFC", "mcdonalds", "pizzahut" ] }

现在top10这个数组刚刚好有3个元素,再次插入两个,则只保留最后三个

> db.updatetest.update({"title":"mail list"},{$push:{"top10":{"$each":["starbucks","illy"],"$slice":-3}}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.updatetest.find(){ "_id" : ObjectId("5625b44d16a7b138cf6bf474"), "title" : "mail list", "Phone" : "010-123456", "emails" : [ "test1@123.com", "test2@124.com", "test3@125.com" ], "top10" : [ "pizzahut", "starbucks", "illy" ] }


对于$push是可以插入重复数据,如果邮件地址已经存在不想再重复插入,则可以使用$addToSet操作,如下:

> db.updatetest.update({"title":"mail list"},... {"$addToSet":{"emails":"test1@123.com"}... })WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })> db.updatetest.find(){ "_id" : ObjectId("5625b44d16a7b138cf6bf474"), "title" : "mail list", "Phone" : "010-123456", "emails" : [ "test1@123.com", "test2@124.com", "test3@125.com" ], "top10" : [ "HP", "DELL", "MicroSoft" ] }> db.updatetest.update({"title":"mail list"}, {"$addToSet":{"emails":{$each:["test1@123.com","abc@321.com","new@newaddress.com","abc@321.com"]}} })WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.updatetest.find(){ "_id" : ObjectId("5625b44d16a7b138cf6bf474"), "title" : "mail list", "Phone" : "010-123456", "emails" : [ "test1@123.com", "test2@124.com", "test3@125.com", "abc@321.com", "new@newaddress.com" ], "top10" : [ "HP", "DELL", "MicroSoft" ] }


基于位置的数组修改:

由一个内嵌文档组成的数组,内容如下:

> db.updatetest.insert({
...
... "content":"Mongodb Update",
... "comments":[
... {
... "comment":"Good post",
... "author":"John",
... "votes":0
... },
... {
... "comment":"too short",
... "author":"mike",
... "votes":3
... },
... {
... "comment":"free watches",
... "author":"Alice",
... "votes":-1
... }
... ]
...
... })

WriteResult({ "nInserted" : 1 })
可以通过$符号来作为定位符,修改comments里面的键,例如修改comments.comment.author 这个键想把Alice改为May操作如下:

> db.updatetest.update({"comments.author":"Alice"},{"$set":{"comments.$.author":"May"}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.updatetest.find()
{ "_id" : ObjectId("5625b44d16a7b138cf6bf474"), "title" : "mail list", "Phone" : "010-123456", "emails" : [ "test1@123.com", "test2@124.com", "test3@125.com", "abc@321.com", "new@newaddress.com" ], "top10" : [ "HP", "DELL", "MicroSoft" ] }
{ "_id" : ObjectId("5625bbef16a7b138cf6bf475"), "content" : "Mongodb Update", "comments" : [ { "comment" : "Good post", "author" : "John", "votes" : 0 }, { "comment" : "too short", "author" : "mike", "votes" : 3 }, { "comment" : "free watches", "author" : "May", "votes" : -1 } ] }



最后一个是findAndModify操作:

该操作可以返回更新前的文档

如下:

> dc=db.runCommand({"findAndModify":"updatetest",
... "query":{"title":"mail list"},
... "sort":{"_id":-1},
... "update":{"$set":{"company":"QQ"}}})
> dc.value
{
"_id" : ObjectId("5625b44d16a7b138cf6bf474"),
"title" : "mail list",
"Phone" : "010-123456",
"emails" : [
"test1@123.com",
"test2@124.com",
"test3@125.com",
"abc@321.com",
"new@newaddress.com"
],
"top10" : [
"HP",
"DELL",
"MicroSoft"
]
}

关于findAndModify操作在update位置的参数可以使用remove,表示匹配query则删除该文档。

还有new,表示返回更新前的文档还是更新后的文档,默认是更新前的。

fields 文档中需要返回的字段(可选)

upsert 布尔值,true表示是一个upsert,默认为false

或者可以这样操作:

> db.updatetest.findAndModify({query:{"title":"mail list"}, update:{$set:{"company":"BBC"}}, new:true, sort:{"_id":-1}, fields:{"emails":1}, upsert:true})


{
"_id" : ObjectId("5625b44d16a7b138cf6bf474"),
"emails" : [
"test1@123.com",
"test2@124.com",
"test3@125.com",
"abc@321.com",
"new@newaddress.com"
]
}
该条语句修改了company为bbc,然后返回字段是emails,new是表示返回更新后的数据.

0