在MongoDB中, update()方法用于更新或修改集合的现有文档。
句法:
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
例子
考虑一个具有集合名称srcmini的示例。在集合中插入以下文档:
db.srcmini.insert(
{
course: "java", details: {
duration: "6 months", Trainer: "Sonoo jaiswal"
}, Batch: [ { size: "Small", qty: 15 }, { size: "Medium", qty: 25 } ], category: "Programming language"
}
)
成功插入后, 通过以下查询检查文档:
>db.srcmini.find()
输出
{ "_id" : ObjectId("56482d3e27e53d2dbc93cef8"), "course" : "java", "details" :
{ "duration" : "6 months", "Trainer" : "Sonoo jaiswal" }, "Batch" :
[ {"size" : "Small", "qty" : 15 }, { "size" : "Medium", "qty" : 25 } ], "category" : "Programming language" }
将现有课程” java”更新为” android”:
>db.srcmini.update({'course':'java'}, {$set:{'course':'android'}})
检查集合中的更新文档:
>db.srcmini.find()
输出
{ "_id" : ObjectId("56482d3e27e53d2dbc93cef8"), "course" : "android", "details" :
{ "duration" : "6 months", "Trainer" : "Sonoo jaiswal" }, "Batch" :
[ {"size" : "Small", "qty" : 15 }, { "size" : "Medium", "qty" : 25 } ], "category" : "Programming language" }
评论前必须登录!
注册