update
UK[??p?de?t] US[?p?det]
vt. Update, modernize; correct, amend
n. Modernize; Updated information; updated behavior or instances
MongoDB update() method syntax
Function: update() method to update documents in the collection
Syntax: db.collection.update(<query>,<update> , {upsert: <boolean>,multi: <boolean>,writeConcern: <document>})
##Parameters: query: update query conditions, similar to sql behind where in the update query. update: the update object and some update operators (such as $, $inc...), etc., can also be understood as the upsert after set in the sql update query: optional, this parameter means that if there is no update Record, whether to insert objNew, true means insert, the default is false, not insert. multi: Optional, the default value of mongodb is false, and only the first record found is updated. If this parameter is true, all multiple records found according to the conditions will be updated. writeConcern: Optional, the level at which the exception is thrown.
MongoDB update() method example
我們?cè)诩?nbsp;col 中插入如下數(shù)據(jù): >db.col.insert({ title: 'MongoDB 教程', description: 'MongoDB 是一個(gè) Nosql 數(shù)據(jù)庫', by: 'php中文網(wǎng)', url: 'http://miracleart.cn', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }) 接著我們通過 update() 方法來更新標(biāo)題(title): >db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) # 輸出信息 > db.col.find().pretty() { "_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一個(gè) Nosql 數(shù)據(jù)庫", "by" : "php中文網(wǎng)", "url" : "http://miracleart.cn", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } > 可以看到標(biāo)題(title)由原來的 "MongoDB 教程" 更新為了 "MongoDB"。 以上語句只會(huì)修改第一條發(fā)現(xiàn)的文檔,如果你要修改多條相同的文檔,則需要設(shè)置 multi 參數(shù)為 true。 >db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})