让我们首先用文档创建一个集合-
>db.replacingEntireDocumentDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Smith","StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3119bb64f4b851c3a13e8") }
以下是在find()
方法的帮助下显示集合中文档的查询-
> db.replacingEntireDocumentDemo.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5cd3119bb64f4b851c3a13e8"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentCountryName" : "US" }
以下是在替换整个文档的同时更新MongoDB文档的查询-
>db.replacingEntireDocumentDemo.update({"StudentFirstName":"John","StudentLastName":"Smith","StudentCountryName":"US"}, {"StudentFirstName":"David","StudentLastName":"Miller","StudentCountryName":"AUS"}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
让我们再次显示集合中的所有记录-
> db.replacingEntireDocumentDemo.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5cd3119bb64f4b851c3a13e8"), "StudentFirstName" : "David", "StudentLastName" : "Miller", "StudentCountryName" : "AUS" }