mongodb-基础-update-remove
1.一些操作
collection重命名:
> db.post.renameCollection('foo') { "ok" : 1 } > show collections foo
2.update
语法格式:
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> })
参数说明:
- query : update的查询条件,类似sql update查询内where后面的。
- update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
- upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
- multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
- 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 老鼠" }
3.save() 方法
save() 方法通过传入的文档来替换已有文档。语法格式如下:
db.collection.save(<document>,{ writeConcern:<document>})
参数说明:
- document : 文档数据。
- writeConcern :可选,抛出异常的级别
如果传入的文档中含有_id,并且_id的值是数据库中已经存在的,则替换该条记录。
如果传入的文档中含有_id,并且_id的值在数据库中不存在,则相当于insert,插入一条数据。
如果传入的文档中没有_id,则相当于insert,插入一条数据。
示例:
数据库中元数据:
> 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:'runn ing' }) 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" }
更多实例
只更新第一条记录:
db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );
全部更新:
db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );
只添加第一条:
db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );
全部添加加进去:
db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );
全部更新:
db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );
只更新第一条记录:
db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );
4.remove
语法
remove() 方法的基本语法格式如下所示:
db.collection.remove(<query>,<justOne>)
如果你的 MongoDB 是 2.6 版本以后的,语法格式如下:
db.collection.remove(<query>,{ justOne:<boolean>, writeConcern:<document>})
参数说明:
- query :(可选)删除的文档的条件。
- justOne : (可选)如果设为 true 或 1,则只删除一个文档。
- 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.c om', 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 命令.
1.一些操作
collection重命名:
> db.post.renameCollection('foo')
{ "ok" : 1 }
> show collections
foo
2.update
语法格式:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
})
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
})
参数说明:
- query : update的查询条件,类似sql update查询内where后面的。
- update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
- upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
- multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
- 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 老鼠"
}
3.save() 方法
save() 方法通过传入的文档来替换已有文档。语法格式如下:
db.collection.save(<document>,{ writeConcern:<document>})
参数说明:
- document : 文档数据。
- writeConcern :可选,抛出异常的级别
如果传入的文档中含有_id,并且_id的值是数据库中已经存在的,则替换该条记录。
如果传入的文档中含有_id,并且_id的值在数据库中不存在,则相当于insert,插入一条数据。
如果传入的文档中没有_id,则相当于insert,插入一条数据。
示例:
数据库中元数据:
> 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:'runn
ing' })
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"
}
更多实例
只更新第一条记录:
db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );
全部更新:
db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );
只添加第一条:
db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );
全部添加加进去:
db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );
全部更新:
db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );
只更新第一条记录:
db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );
4.remove
语法
remove() 方法的基本语法格式如下所示:
db.collection.remove(<query>,<justOne>)
如果你的 MongoDB 是 2.6 版本以后的,语法格式如下:
db.collection.remove(<query>,{ justOne:<boolean>, writeConcern:<document>})
参数说明:
- query :(可选)删除的文档的条件。
- justOne : (可选)如果设为 true 或 1,则只删除一个文档。
- 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.c
om', 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 命令.
关注我的公众号
![](https://images2018.cnblogs.com/blog/686418/201808/686418-20180822091328437-1109977663.jpg)