在替换整个文档时如何更新MongoDB文档?
让我们首先用文档创建一个集合-
>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"
}热门推荐
10 古风婚礼祝福语唯美简短
11 贺卡祝福语宝宝男孩简短
12 项目建设春节祝福语简短
13 新婚祝福语简短十六字
14 新婚抖音祝福语简短
15 小寒健康祝福语大全简短
16 给老板祝福语简短精辟
17 简短有内涵的祝福语
18 公司文案生日祝福语简短