MongoDB从入门到实战(五):MongoDB 修改文档 update

x33g5p2x  于2021-12-25 转载在 Go  
字(8.8k)|赞(0)|评价(0)|浏览(557)

db.collection.update(query, update)

整体替换。

> db.user.find()
{ "_id" : 1, "username" : "a" }
{ "_id" : 2, "username" : "b" }

// update默认是整体替换掉满足条件的文档而不是单独的修改指定字段的值
> db.user.update({"_id": 1}, {"age": 30})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find()
{ "_id" : 1, "age" : 30 }
{ "_id" : 2, "username" : "b" }

// update(query, update) : 默认修改满足条件的第一条文档
> db.user.find()
{ "_id" : 1, "username" : "a" }
{ "_id" : 2, "username" : "a" }
> db.user.update({"username": "a"}, {"username": "aa"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "username" : "aa" }
{ "_id" : 2, "username" : "a" }

db.collection.update(query, update, insertOrUpdate)

// 不存在则添加
> db.user.find()
> db.user.update({"_id": 1}, {"username": "addOrUpdate"}, true)
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1 })
> db.user.find()
{ "_id" : 1, "username" : "addOrUpdate" }

// 存在则更新
> db.user.update({"_id": 1}, {"username": "Update"}, true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "username" : "Update" }

修饰器

$set

局部修改,修改指定字段的值。

// 使用 $set修改器 修改指定字段, 当字段不存在时会创建并赋值
> db.user.find()
{ "_id" : 1, "username" : "a" }
{ "_id" : 2, "username" : "b" }

// 字段存在则修改,不存在则添加字段
> db.user.update({"_id": 1}, {"$set": {"username": "aa", "age": 30}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "username" : "aa", "age" : 30 }
{ "_id" : 2, "username" : "b" }

db.collection.update(query, update, insertOrUpdate, multiUpdate)

批量更新只适用于局部替换,不能使用全局覆盖。

> db.user.find()
{ "_id" : 1, "username" : "a" }
{ "_id" : 2, "username" : "a" }

// 批量 局部更新
> db.user.update({"username": "a"}, {"$set": {"username": "aaa"}}, false, true)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.user.find()
{ "_id" : 1, "username" : "aaa" }
{ "_id" : 2, "username" : "aaa" }

$unset

用于删除字段。

// 删除age字段
> db.user.update({"_id": 1}, {"$unset": {"age": 1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "username" : "aa" }
{ "_id" : 2, "username" : "b" }

$inc

用于递增或者递减某个值,值为整数表示递增,值为负数表示递减。

> db.user.find()
{ "_id" : 1, "age" : 29 }
{ "_id" : 2, "age" : 30 }
> db.user.update({_id: 1}, {"$inc": {"age": 2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "age" : 31 }
{ "_id" : 2, "age" : 30 }

> db.user.update({_id: 1}, {"$inc": {"age": -2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "age" : 29 }
{ "_id" : 2, "age" : 30 }

$push

向数组字段的尾部添加一个元素,如果字段不存在则创建。

> db.user.find()
{ "_id" : 1, "username" : "aa" }
{ "_id" : 2, "username" : "b" }

// 文档不存在hobby字段就添加,类型为数组
> db.user.update({"_id": 1}, {"$push": {"hobby": "money"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "username" : "aa", "hobby" : [ "money" ] }
{ "_id" : 2, "username" : "b" }

// 文档hobby存在字段,就向数组尾部添加元素
> db.user.update({"_id": 1}, {"$push": {"hobby": "xiaojiejie"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "username" : "aa", "hobby" : [ "money", "xiaojiejie" ] }
{ "_id" : 2, "username" : "b" }

$push + $each

向数组字段的尾部一次性添加多个元素。

> db.user.find()
{ "_id" : 1, "username" : "a" }
{ "_id" : 2, "username" : "b" }

// 一次性向数组字段的尾部添加多个元素
> db.user.update({"_id": 1}, {"$push": {"hobby": {"$each": ["money", "xiaojiejie"]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find()
{ "_id" : 1, "username" : "a", "hobby" : [ "money", "xiaojiejie" ] }
{ "_id" : 2, "username" : "b" }

$addToSet

想数组尾部添加一个不存在的元素,如果存在则不添加。

> db.user.find()
{ "_id" : 1, "username" : "a" }
{ "_id" : 2, "username" : "b" }

// 第一次添加成功
> db.user.update({"_id": 1}, {"$addToSet": {"hobby": "money"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : 1, "username" : "a", "hobby" : [ "money" ] }
{ "_id" : 2, "username" : "b" }

// 第二次添加失败
> db.user.update({"_id": 1}, {"$addToSet": {"hobby": "money"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.user.find()
{ "_id" : 1, "username" : "a", "hobby" : [ "money" ] }
{ "_id" : 2, "username" : "b" }

$pop

弹出数组的头部元素或尾部元素: -1:头部,1:尾部。

> db.user.find()
{ "_id" : 1, "username" : "a", "hobby" : [ "money", "xiaojiejie", "dog" ] }
{ "_id" : 2, "username" : "b" }

> db.user.update({"_id": 1}, {"$pop": {"hobby": 1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find()
{ "_id" : 1, "username" : "a", "hobby" : [ "money", "xiaojiejie" ] }
{ "_id" : 2, "username" : "b" }
>

$pull

删除数组中的指定的值。

> db.user.find()
{ "_id" : 1, "username" : "a", "hobby" : [ "money", "xiaojiejie", "dog" ] }
{ "_id" : 2, "username" : "b" }

// 删除指定的元素
> db.user.update({"_id": 1}, {"$pull": {"hobby": "xiaojiejie"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find()
{ "_id" : 1, "username" : "a", "hobby" : [ "money", "dog" ] }
{ "_id" : 2, "username" : "b" }

使用下标修改数组中指定的元素

> db.user.insert({ "_id" : 1, "addresses" : [ { "city" : "shanghai", "area" : "pudong" }, { "city" : "beijing", "area" : "chaoyang" } ] })

// 修改addresses字段中的第二个元素中的area字段的值
> db.user.update({"_id": 1}, {"$set": {"addresses.1.area": "dongcheng"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find()
{ "_id" : 1, 
 "addresses" : [ 
 		{ "city" : "shanghai", "area" : "pudong" }, 
		{ "city" : "beijing", "area" : "dongcheng" } 
	] 
}
// $ 表示满足条件所对应的下标
> db.user.update({"addresses.city": "shanghai"}, {"$set": {"addresses.$.area": "Pu Dong"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find().pretty()
{
	"_id" : 1,
	"addresses" : [
		{
			"city" : "shanghai",
			"area" : "Pu Dong"
		},
		{
			"city" : "beijing",
			"area" : "dongcheng"
		}
	]
}

db.collection.findAndModify({ “query”: xxx, “update”: xxx)

findAndModify : 查找满足添加的值并返回,并且修改满足条件的第一条文档。

> db.user.find()
{ "_id" : 1, "username" : "a" }
{ "_id" : 2, "username" : "a" }
> db.user.findAndModify({"query": {"username": "a"}, "update": {"username": "aa"}})
{ "_id" : 1, "username" : "a" }

> db.user.find()
{ "_id" : 1, "username" : "aa" }
{ "_id" : 2, "username" : "a" }

相关文章