更新 MongoDB 中的数据
要在 MongoDB 中更新数据,请使用 update()。我们使用文档创建集合 -
> db.demo110.insertOne({"Name":"Chris"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2eeb949fd5fd66da21447b")
}
> db.demo110.insertOne({"Name":"David"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2eeb9f9fd5fd66da21447c")
}
> db.demo110.insertOne({"Name":"Bob"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2eeba39fd5fd66da21447d")
}使用 find() 方法显示集合中的所有文档 -
> db.demo110.find();
这将产生以下输出 -
{ "_id" : ObjectId("5e2eeb949fd5fd66da21447b"), "Name" : "Chris" }
{ "_id" : ObjectId("5e2eeb9f9fd5fd66da21447c"), "Name" : "David" }
{ "_id" : ObjectId("5e2eeba39fd5fd66da21447d"), "Name" : "Bob" }以下是更新 MongoDB 中数据的查询 -
>db.demo110.update({_id:ObjectId("5e2eeb9f9fd5fd66da21447c")},{$set:{"Name":"Robert"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })使用 find() 方法显示集合中的所有文档 -
> db.demo110.find();
这将产生以下输出 -
{ "_id" : ObjectId("5e2eeb949fd5fd66da21447b"), "Name" : "Chris" }
{ "_id" : ObjectId("5e2eeb9f9fd5fd66da21447c"), "Name" : "Robert" }
{ "_id" : ObjectId("5e2eeba39fd5fd66da21447d"), "Name" : "Bob" }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP