之前没记录更新的操作,今天有空记录一下mongodb update的使用
先看语法:
db.collection.update( <query>, <update>, <options> )
语法很简单,但是使用起来,要配合一些函数,就会复杂一些,解释一下
query:条件限定,也就是需要更新行的条件,相当于SQL中where后面的条件判断
update:相当于SQL表中的set语句
options:这里需要注意的是mongodb的更新操作,默认只更新符合条件的第一行,如果要更新符合条件的所有行,需要指定multi为true,还有一个一个参数是upsert,意思是如果不存在符合query的记录,插入update的数据,默认是false不插入,可以指定true
最简单的一个例子,大家看一下:
PRIMARY> db.test.save({id:1,"name":"hank"})
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c055656b41f59a0235881b"), "id" : 1, "name" : "hank" }
PRIMARY> db.test.update({id:1},{"name":"dazuiba"})
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c055656b41f59a0235881b"), "name" : "dazuiba" }
默认更新一行例子:
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c056876b41f59a0235881c"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c056886b41f59a0235881d"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c056896b41f59a0235881e"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c0568a6b41f59a0235881f"), "id" : 1, "name" : "hank" }
PRIMARY> db.test.update({id:1},{"name":"dazuiba"})
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c056876b41f59a0235881c"), "name" : "dazuiba" }
{ "_id" : ObjectId("50c056886b41f59a0235881d"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c056896b41f59a0235881e"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c0568a6b41f59a0235881f"), "id" : 1, "name" : "hank" }
更新所有符合条件的行:
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c056e46b41f59a02358820"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c056e56b41f59a02358821"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c056e66b41f59a02358822"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c056e86b41f59a02358823"), "id" : 1, "name" : "hank" }
PRIMARY> db.test.update({id:1},{"name":"dazuiba"},false,true) --报错,大家注意一下这里,需要$相关的操作
multi update only works with $ operators
PRIMARY> db.test.update({id:1},{$set:{"name":"dazuiba"}},false,true)
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c056e46b41f59a02358820"), "id" : 1, "name" : "dazuiba" }
{ "_id" : ObjectId("50c056e56b41f59a02358821"), "id" : 1, "name" : "dazuiba" }
{ "_id" : ObjectId("50c056e66b41f59a02358822"), "id" : 1, "name" : "dazuiba" }
{ "_id" : ObjectId("50c056e86b41f59a02358823"), "id" : 1, "name" : "dazuiba" }
更新不存在的行,如果不存在,那么插入进去:
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c058256b41f59a02358828"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c058266b41f59a02358829"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c058266b41f59a0235882a"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c058276b41f59a0235882b"), "id" : 1, "name" : "hank" }
PRIMARY> db.test.update({id:2},{$set:{"name":"dazuiba"}},true,false)
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c058256b41f59a02358828"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c058266b41f59a02358829"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c058266b41f59a0235882a"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c058276b41f59a0235882b"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c05840638c411993e87a5c"), "id" : 2, "name" : "dazuiba" }
只要理解了2个参数意思,灵活使用即可,以下是几个例子
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c0589e6b41f59a0235882c"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c058a06b41f59a0235882d"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c058a16b41f59a0235882e"), "id" : 1, "name" : "hank" }
{ "_id" : ObjectId("50c058a16b41f59a0235882f"), "id" : 1, "name" : "hank" }
PRIMARY> db.test.update({id:1},{$set:{"name":"dazuiba"}},true,true)
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c0589e6b41f59a0235882c"), "id" : 1, "name" : "dazuiba" }
{ "_id" : ObjectId("50c058a06b41f59a0235882d"), "id" : 1, "name" : "dazuiba" }
{ "_id" : ObjectId("50c058a16b41f59a0235882e"), "id" : 1, "name" : "dazuiba" }
{ "_id" : ObjectId("50c058a16b41f59a0235882f"), "id" : 1, "name" : "dazuiba" }
PRIMARY> db.test.update({id:2},{$set:{"name":"hank"}},true,true)
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c0589e6b41f59a0235882c"), "id" : 1, "name" : "dazuiba" }
{ "_id" : ObjectId("50c058a06b41f59a0235882d"), "id" : 1, "name" : "dazuiba" }
{ "_id" : ObjectId("50c058a16b41f59a0235882e"), "id" : 1, "name" : "dazuiba" }
{ "_id" : ObjectId("50c058a16b41f59a0235882f"), "id" : 1, "name" : "dazuiba" }
{ "_id" : ObjectId("50c058ea638c411993e87a5d"), "id" : 2, "name" : "hank" }
$相关操作下一次介绍