时间:2021-07-01 10:21:17 帮助过:35人阅读
前面我们已经介绍了mongodb的基本操作:mongodb基本操作下面是介绍一些高级的增删改查操作. 1.更新文档 文档替换 最简单的方法就是文档完全替换,如下就是一个完全替换的例子,先把需要更新的文档找出来并且赋,然后修改所赋的,最后再进行update: a1 = db . test
前面我们已经介绍了mongodb的基本操作:mongodb基本操作 下面是介绍一些高级的增删改查操作.> a1=db.test.findOne()
{ "_id" : ObjectId("5738785d132e1e47e535a177"), "x" : 3.14, "y" : 5.5 }
> a1
{ "_id" : ObjectId("5738785d132e1e47e535a177"), "x" : 3.14, "y" : 5.5 }
> delete a1.x
true
> a1
{ "_id" : ObjectId("5738785d132e1e47e535a177"), "y" : 5.5 }
> a1.content={name:'job',age:11}
{ "name" : "job", "age" : 11 }
> a1
{
"_id" : ObjectId("5738785d132e1e47e535a177"),
"y" : 5.5,
"content" : {
"name" : "job",
"age" : 11
}
}
> db.test.update({"_id" : ObjectId("5738785d132e1e47e535a177")},a1)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
然后用修改器再将phone该为888> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com" }
>
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$set":{phone:666}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com", "phone" : 666 }
使用"$set"修改器还可以修改字段的属性,例如可以把phone改成一个数组,还可以用"$unset"将键完全删除:> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$set":{phone:888}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com", "phone" : 888 }
使用"$set"修改内嵌文档:
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$unset":{phone:888}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com" }
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com" }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 888, "address" : "abc" } }
>
> db.test.update({"_id" : ObjectId("57395fa85f74882a9bfa2d9f")},{"$set":{"id.phone":666}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com" }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 666, "address" : "abc" } }
如果要减少,则将age:1里的1改成-1> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 30, "email" : "xxxx@qq.com" }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 666, "address" : "abc" } }
> db.test.update({"_id" : ObjectId("573957c55f74882a9bfa2d9e")},{"$inc":{age:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.find()
{ "_id" : ObjectId("573957c55f74882a9bfa2d9e"), "name" : "brent", "age" : 31, "email" : "xxxx@qq.com" }
{ "_id" : ObjectId("57395fa85f74882a9bfa2d9f"), "id" : { "phone" : 666, "address" : "abc" } }