使用update()更新数据
db.collection.update(critera,objNEW,options)
updates函数接受3个参数:
- critera:指定查询,选择将要更新的文档;
- objNEW:指定更新信息,也可用操作符完成;
- options:指定更新文档时的选项,可选值有upsert和multi.
upsert:如果数据存在就更新,否则创建数据。
multi:指定是否更新所有匹配文档,或者只更新第一个匹配的文档(默认行为)。
db.media.find()
{ "_id" : ObjectId("585f822891c851f743fea7b5"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }
{ "_id" : ObjectId("585f824d91c851f743fea7b6"), "Type" : "DVD", "Title" : "Tony Story3", "Released" : 2010 }
{ "_id" : ObjectId("585f828391c851f743fea7b7"), "Type" : "DVD", "Title" : "Matrix", "Released" : 1999 }
db.media.update({Title:'Blade Runner'},{Type:'Book',Title:'Wars and Peace'},{upsert:true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.media.find()
{ "_id" : ObjectId("585f822891c851f743fea7b5"), "Type" : "Book", "Title" : "Wars and Peace" }
{ "_id" : ObjectId("585f824d91c851f743fea7b6"), "Type" : "DVD", "Title" : "Tony Story3", "Released" : 2010 }
{ "_id" : ObjectId("585f828391c851f743fea7b7"), "Type" : "DVD", "Title" : "Matrix", "Released" : 1999 }
该更新操作替换原先的文档,并保存新的文档
设置字段的值
db.media.update({Title:'Wars and Peace'},{$set:{Released:2015}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
{ "_id" : ObjectId("585f822891c851f743fea7b5"), "Type" : "Book", "Title" : "Wars and Peace", "Released" : 2015 }
{ "_id" : ObjectId("585f824d91c851f743fea7b6"), "Type" : "DVD", "Title" : "Tony Story3", "Released" : 2010 }
{ "_id" : ObjectId("585f828391c851f743fea7b7"), "Type" : "DVD", "Title" : "Matrix", "Released" : 1999 }
删除指定的字段
db.media.update({Title:'Wars and Peace'},{$unset:{Released:2015}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.media.find()
{ "_id" : ObjectId("585f822891c851f743fea7b5"), "Type" : "Book", "Title" : "Wars and Peace" }
{ "_id" : ObjectId("585f824d91c851f743fea7b6"), "Type" : "DVD", "Title" : "Tony Story3", "Released" : 2010 }
{ "_id" : ObjectId("585f828391c851f743fea7b7"), "Type" : "DVD", "Title" : "Matrix", "Released" : 1999 }
在指定的字段中添加某个值
通过$push操作符可以在指定字段中添加某个值。
* 如果该字段是个数组,该值将被添加到数组中;
* 如果该字段尚不存在,该字段的值将被设置为数组;
* 如果该字段存在,但不是数组,将会抛出错误。
db.media.update({Title:'Wars and Peace'},{$push:{Author:'Tony Stewie'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.media.find()
{ "_id" : ObjectId("585f822891c851f743fea7b5"), "Type" : "Book", "Title" : "Wars and Peace", "Author" : [ "Tony Stewie" ] }
{ "_id" : ObjectId("585f824d91c851f743fea7b6"), "Type" : "DVD", "Title" : "Tony Story3", "Released" : 2010 }
{ "_id" : ObjectId("585f828391c851f743fea7b7"), "Type" : "DVD", "Title" : "Matrix", "Released" : 1999 }
指定数组中的多个值
db.media.update({Title:'Wars and Peace'},{$push:{Author:{$each:['John Smith','Andy Fox']}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.media.find()
{ "_id" : ObjectId("585f822891c851f743fea7b5"), "Type" : "Book", "Title" : "Wars and Peace", "Author" : [ "Tony Stewie", "John Smith", "Andy Fox" ] }
{ "_id" : ObjectId("585f824d91c851f743fea7b6"), "Type" : "DVD", "Title" : "Tony Story3", "Released" : 2010 }
{ "_id" : ObjectId("585f828391c851f743fea7b7"), "Type" : "DVD", "Title" : "Matrix", "Released" : 1999 }
删除数组中第一个或最后一个元素
删除数组中最后一个元素
db.media.update({Title:'Wars and Peace'},{$pop:{Author:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
删除数组中第一个元素
db.media.update({Title:'Wars and Peace'},{$pop:{Author:-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
删除所有指定的值
db.media.update({Title:'Wars and Peace'},{$pull:{Author:'Tony Stewie'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
删除数组中的多个值
db.media.find()
{ "_id" : ObjectId("585f822891c851f743fea7b5"), "Type" : "Book", "Title" : "Wars and Peace", "Author" : [ "John Smith", "Tony Stewie" ] }
{ "_id" : ObjectId("585f824d91c851f743fea7b6"), "Type" : "DVD", "Title" : "Tony Story3", "Released" : 2010 }
{ "_id" : ObjectId("585f828391c851f743fea7b7"), "Type" : "DVD", "Title" : "Matrix", "Released" : 1999 }
db.media.update({Title:'Wars and Peace'},{$pullAll:{Author:['John Smith','Tony Stewie']}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.media.find()
{ "_id" : ObjectId("585f822891c851f743fea7b5"), "Type" : "Book", "Title" : "Wars and Peace", "Author" : [ ] }
{ "_id" : ObjectId("585f824d91c851f743fea7b6"), "Type" : "DVD", "Title" : "Tony Story3", "Released" : 2010 }
{ "_id" : ObjectId("585f828391c851f743fea7b7"), "Type" : "DVD", "Title" : "Matrix", "Released" : 1999 }