MongoDB中如何做到更新并防止覆盖
> db.demo601.insertOne(
... {
... id:1,
... userDetails:
... {
... userName:"John",
... userMailId:"John@gmail.com"
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e95ff5ced011c280a0905c7")
}
>
> db.demo601.insertOne( { id:2, userDetails: { userName:"Carol",
userMailId:"Carol@gmail.com" } } );{
"acknowledged" : true,
"insertedId" : ObjectId("5e95ff71ed011c280a0905c8")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo601.find();
这将产生以下输出-
{ "_id" : ObjectId("5e95ff5ced011c280a0905c7"), "id" : 1, "userDetails" : { "userName" : "John", "userMailId" : "John@gmail.com" } }
{ "_id" : ObjectId("5e95ff71ed011c280a0905c8"), "id" : 2, "userDetails" : { "userName" : "Carol", "userMailId" : "Carol@gmail.com" } }以下是要更新的查询-
>db.demo601.update({_id:ObjectId("5e95ff71ed011c280a0905c8")},{$set:{userMailId:"Carol@yahoo.com"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })在find()方法的帮助下显示集合中的所有文档-
> db.demo601.find();
这将产生以下输出-
{ "_id" : ObjectId("5e95ff5ced011c280a0905c7"), "id" : 1, "userDetails" :
{ "userName" : "John", "userMailId" : "John@gmail.com" }
}
{ "_id" : ObjectId("5e95ff71ed011c280a0905c8"), "id" : 2, "userDetails" :
{ "userName" : "Carol", "userMailId" : "Carol@gmail.com" }, "userMailId" : "Carol@yahoo.com"
}热门推荐
10 庆双节祝福语简短
11 公司开年仪式祝福语简短
12 六一婚礼文案祝福语简短
13 祝女性生日祝福语简短
14 高考前祝福语简短字句
15 中秋妈妈生日祝福语简短
16 送蛇的祝福语简短
17 简短的出行工作祝福语
18 姐姐女儿结婚祝福语简短