我们如何更新 MongoDB 中的一条记录?
更新记录时,你需要以 _id 作依据。让我们创建一个包含文档的集合 -
> db.demo458.insertOne( {_id:101,"Name":"David" } );
{ "acknowledged" : true, "insertedId" : 101 }
> db.demo458.insertOne( {_id:102,"Name":"Chris" } );
{ "acknowledged" : true, "insertedId" : 102 }
> db.demo458.insertOne( {_id:103,"Name":"Bob" } );
{ "acknowledged" : true, "insertedId" : 103 }在集合中显示所有文档 - find() 方法
> db.demo458.find();
这将产生以下输出 -
{ "_id" : 101, "Name" : "David" }
{ "_id" : 102, "Name" : "Chris" }
{ "_id" : 103, "Name" : "Bob" }以下是更新 MongoDB 中记录的查询 -
> db.demo458.update({_id:102},{$set:{"Name":"David Miller"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })在集合中显示所有文档 - find() 方法
> db.demo458.find();
这将产生以下输出 -
{ "_id" : 101, "Name" : "David" }
{ "_id" : 102, "Name" : "David Miller" }
{ "_id" : 103, "Name" : "Bob" }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP