本文共 16137 字,大约阅读时间需要 53 分钟。
> db.post.renameCollection('foo'){ "ok" : 1 }> show collectionsfoo
db.collection.update(, , { upsert: , multi: , writeConcern: })
参数说明:
示例:
>db.foo.insert([ {"title":"MongoDB Overview"}, {"title":"NoSQL Overview"}, {"title":"Test Update"}])> db.foo.find(){ "_id" : ObjectId("577f11860fe1ae602eda933a"), "title" : "MongoDB Overview" }{ "_id" : ObjectId("577f11860fe1ae602eda933b"), "title" : "NoSQL Overview" }{ "_id" : ObjectId("577f11860fe1ae602eda933c"), "title" : "Test Update" }>> db.foo.update(... {'title':'MongoDB Overview'},... {$set:{'title':'New MongoDB Overview'}}... )WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })>> db.foo.find(){ "_id" : ObjectId("577f11860fe1ae602eda933a"), "title" : "New MongoDB Overview" }{ "_id" : ObjectId("577f11860fe1ae602eda933b"), "title" : "NoSQL Overview" }{ "_id" : ObjectId("577f11860fe1ae602eda933c"), "title" : "Test Update" }>
测试更新多个:
# 初始化数据> db.post.insert([{'username':'Ryan','age':18,'sex':0,'hobby':'read'},{'username':'Leslie','age':18,'sex':1,'hobby':'watch TV'},{'username':'Tom','age':20,'sex':0,'hobby':'eat 老鼠'}])BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ]})> db.post.find().pretty(){ "_id" : ObjectId("577f145a0fe1ae602eda933d"), "username" : "Ryan", "age" : 18, "sex" : 0, "hobby" : "read"}{ "_id" : ObjectId("577f145a0fe1ae602eda933e"), "username" : "Leslie", "age" : 18, "sex" : 1, "hobby" : "watch TV"}{ "_id" : ObjectId("577f145a0fe1ae602eda933f"), "username" : "Tom", "age" : 20, "sex" : 0, "hobby" : "eat 老鼠"}
结果发现,这种更新只能更新匹配的第一条记录:
#更新age=18的为age=20> db.post.update(... {'age':18},... {$set:{'age':20}}... )WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.post.find().pretty(){ "_id" : ObjectId("577f145a0fe1ae602eda933d"), "username" : "Ryan", "age" : 20, "sex" : 0, "hobby" : "read"}{ "_id" : ObjectId("577f145a0fe1ae602eda933e"), "username" : "Leslie", "age" : 18, "sex" : 1, "hobby" : "watch TV"}{ "_id" : ObjectId("577f145a0fe1ae602eda933f"), "username" : "Tom", "age" : 20, "sex" : 0, "hobby" : "eat 老鼠"}
如果想要更新符合条件的所有的记录,需要设置multi:true:
# 更新age=20 为 age=22> db.post.update( {'age':20}, {$set:{'age':22}},{multi:true} )WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })> db.post.find().pretty(){ "_id" : ObjectId("577f145a0fe1ae602eda933d"), "username" : "Ryan", "age" : 22, "sex" : 0, "hobby" : "read"}{ "_id" : ObjectId("577f145a0fe1ae602eda933e"), "username" : "Leslie", "age" : 18, "sex" : 1, "hobby" : "watch TV"}{ "_id" : ObjectId("577f145a0fe1ae602eda933f"), "username" : "Tom", "age" : 22, "sex" : 0, "hobby" : "eat 老鼠"}
save() 方法通过传入的文档来替换已有文档。语法格式如下:
db.collection.save(,{ writeConcern: })
参数说明:
> db.post.find().pretty(){ "_id" : ObjectId("577f145a0fe1ae602eda933d"), "username" : "Ryan", "age" : 22, "sex" : 0, "hobby" : "read"}{ "_id" : ObjectId("577f145a0fe1ae602eda933e"), "username" : "Leslie", "age" : 18, "sex" : 1, "hobby" : "watch TV"}{ "_id" : ObjectId("577f145a0fe1ae602eda933f"), "username" : "Tom", "age" : 22, "sex" : 0, "hobby" : "eat 老鼠"}
替换最后一条记录:
> db.post.save({... "_id" : ObjectId("577f145a0fe1ae602eda933f"),... "username":"Jack",... "age":30,... sex:1,... hobby:'running'... })WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.post.find().pretty(){ "_id" : ObjectId("577f145a0fe1ae602eda933d"), "username" : "Ryan", "age" : 22, "sex" : 0, "hobby" : "read"}{ "_id" : ObjectId("577f145a0fe1ae602eda933e"), "username" : "Leslie", "age" : 18, "sex" : 1, "hobby" : "watch TV"}{ "_id" : ObjectId("577f145a0fe1ae602eda933f"), "username" : "Jack", "age" : 30, "sex" : 1, "hobby" : "running"}
如果没有指定_id,相当于insert:
> db.post.save({ "username":"Mary", "age":28, sex:0, hobby:'sleep' })WriteResult({ "nInserted" : 1 })> db.post.find().pretty(){ "_id" : ObjectId("577f145a0fe1ae602eda933d"), "username" : "Ryan", "age" : 22, "sex" : 0, "hobby" : "read"}{ "_id" : ObjectId("577f145a0fe1ae602eda933e"), "username" : "Leslie", "age" : 18, "sex" : 1, "hobby" : "watch TV"}{ "_id" : ObjectId("577f145a0fe1ae602eda933f"), "username" : "Jack", "age" : 30, "sex" : 1, "hobby" : "running"}{ "_id" : ObjectId("577f1fd30fe1ae602eda9340"), "username" : "Mary", "age" : 28, "sex" : 0, "hobby" : "sleep"}
如果指定了_id,但没有数据库匹配该id,则相当于insert,插入一条数据:
> db.post.save({ "_id" : 123123, "username":"Jack", "age":30, sex:1, hobby:'running' })WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 123123 })> db.post.find().pretty(){ "_id" : ObjectId("577f145a0fe1ae602eda933d"), "username" : "Ryan", "age" : 22, "sex" : 0, "hobby" : "read"}{ "_id" : ObjectId("577f145a0fe1ae602eda933e"), "username" : "Leslie", "age" : 18, "sex" : 1, "hobby" : "watch TV"}{ "_id" : ObjectId("577f145a0fe1ae602eda933f"), "username" : "Jack", "age" : 30, "sex" : 1, "hobby" : "running"}{ "_id" : ObjectId("577f1fd30fe1ae602eda9340"), "username" : "Mary", "age" : 28, "sex" : 0, "hobby" : "sleep"}{ "_id" : 123123, "username" : "Jack", "age" : 30, "sex" : 1, "hobby" : "running"}
只更新第一条记录:
全部更新:
只添加第一条:
全部添加加进去:
全部更新:
只更新第一条记录:
remove() 方法的基本语法格式如下所示:
db.collection.remove(, )
如果你的 MongoDB 是 2.6 版本以后的,语法格式如下:
db.collection.remove(,{ justOne: , writeConcern: })
参数说明:
> db.col.insert({... title:'MongoDB Teach',... by:'菜鸟教程',... url:'http://www.runoob.com',... tags:['mongodb','database','NoSQL'],... likes:100... })WriteResult({ "nInserted" : 1 })> db.col.insert({ title:'MongoDB Teach', by:'菜鸟教程', url:'http://www.runoob.com', tags:['mongodb','database','NoSQL'], likes:100 })WriteResult({ "nInserted" : 1 })>>> db.col.find(){ "_id" : ObjectId("577f52430fe1ae602eda9341"), "title" : "MongoDB Teach", "by": "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }{ "_id" : ObjectId("577f52460fe1ae602eda9342"), "title" : "MongoDB Teach", "by": "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
> db.col.remove({'title':'MongoDB Teach'})WriteResult({ "nRemoved" : 2 })
可以看到,两条都删除了,即:满足条件的都删除。因此,如果想清空表:
> db.col.remove({})
相当于 SQL 的 truncate 命令.
参数说明:
save() 方法通过传入的文档来替换已有文档。语法格式如下:
db.collection.save(,{ writeConcern: })
参数说明:
只更新第一条记录:
全部更新:
只添加第一条:
全部添加加进去:
全部更新:
只更新第一条记录:
remove() 方法的基本语法格式如下所示:
db.collection.remove(, )
如果你的 MongoDB 是 2.6 版本以后的,语法格式如下:
db.collection.remove(,{ justOne: , writeConcern: })
参数说明:
转载地址:http://rmxkl.baihongyu.com/