首页 > 代码库 > MongoDB创建\更新\删除文档操作
MongoDB创建\更新\删除文档操作
一、插入\创建文档
--当插入一个不存在的文档时,会自己主动创建一个文档
[root@racdb ~]# mongo
MongoDB shell version: 2.4.14
connecting to: test
> show collections
> db.cols.insert({bar:"baz"})
> db.cols.find()
{ "_id" :ObjectId("56aac1df4e61b6d9f84d17e0"), "bar" :"baz" }
二、删除文档
--删除全部文档
> db.cols.remove()
--删除符合条件的文档
> db.cols.remove({bar:"baz"})
注意:db.cols.remove()不会删除cols集合本身,原有索引也会保留
三、更新文档
文档替换
--假设把以下文档
>db.users.findOne({"name":"licz"})
{
"_id" : ObjectId("56a8828b308203e00e436b01"),
"name" : "licz",
"friends" : 43,
"enemies" : 5
}
--更新成以下文档
{
"_id" : ObjectId("56a8828b308203e00e436b01"),
"relationships" : {
"friends" : 43,
"enemies" : 5
},
"username" : "licz"
}
更新方法:
> licz.relationships ={"friends":licz.friends,"enemies":licz.enemies}
{ "friends" : 43,"enemies" : 5 }
> licz.username = licz.name
licz
> delete licz.friends
true
> delete licz.enemies
true
> delete licz.name
true
>db.users.findOne({"name":"licz"})
{
"_id" : ObjectId("56a8828b308203e00e436b01"),
"name" : "licz",
"friends" : 43,
"enemies" : 5
}
>db.users.update({name:"licz"},licz)
>db.users.findOne({"username":"licz"})
{
"_id" : ObjectId("56a8828b308203e00e436b01"),
"relationships" : {
"friends" : 43,
"enemies" : 5
},
"username" : "licz"
}
使用改动器
1. $set
$set用来改动指定键的值,假设键不存在,就创建它。
>db.users.findOne({"name":"haley"})
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "haley",
"age" : 30,
"sex" : "male"
}
--增加文档的键值对
> db.users.update({"name":"haley"},{"$set":{"location":"china"}})
> db.users.update({"name":"haley"},{"$set":{"favoritebook":"war and peace"}})
>db.users.findOne({"name":"haley"})
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "haley",
"age" : 30,
"sex" : "male",
"location" : "china",
"favorite book" : "war and peace"
}
--改动"favoritebook"键的值
> db.users.update({"name":"haley"},{"$set":{"favoritebook":"green eggs and ham"}})
>db.users.findOne({"name":"haley"})
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "haley",
"age" : 30,
"sex" : "male",
"location" : "china",
"favorite book" : "green eggs and ham"
}
2. $inc
$inc用来增加/降低文档中键的值,相同假设键不存在,就创建它
>db.analytics.findOne({"url":"www.example.com"})
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 54
}
>db.analytics.update({"url":"www.example.com"},{"$inc":{"pageviews":1}})
>db.analytics.findOne({"url":"www.example.com"})
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 55
}
--增加"visits"键值对
>db.analytics.update({"url":"www.example.com"},{"$inc":{"visits":3}})
>db.analytics.findOne({"url":"www.example.com"})
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 55,
"visits" : 3
}
注意:能够看$set和$inc改动器的差别:
$set是改动字符型的键值,$inc是改动数值型的键值;都是在不存在键时会自己主动增加上。
数据组改动器
3. $push
$push作用:假设指定的键存在,$push会向已有数组末尾增加一个元素,要是没有就会创建一个新的数据。
>db.blog.posts.findOne({"title":"A Oracle error summary"})
{
"_id" :ObjectId("56aad2744e61b6d9f84d17e1"),
"title" : "A Oracle error summary",
"content" : "..."
}
> db.blog.posts.update({"title":"AOracle error summary"},
...{"$push":{"comments":{"name":"licz","email":"licz@163.com","content":"goodpost!"}}})
>db.blog.posts.findOne({"title":"A Oracle error summary"})
{
"_id" : ObjectId("56aad2744e61b6d9f84d17e1"),
"title" : "A Oracle error summary",
"content" :"...",
"comments" : [
{
"name" :"licz",
"email" :"licz@163.com",
"content" :"good post!"
}
]
}
--再次加一个数据元素
... {"$push":{"comments":{"name":"haley","email":"haley@qq.com","content":"thankyou post"}}})
>db.blog.posts.findOne({"title":"A Oracle error summary"})
{
"_id" : ObjectId("56aad2744e61b6d9f84d17e1"),
"title" : "A Oracle error summary",
"content" : "...",
"comments" : [
{
"name" :"licz",
"email" :"licz@163.com",
"content" :"good post!"
},
{
"name" :"haley",
"email" :"haley@qq.com",
"content" :"thank you post"
}
]
}
4. $ne
$ne能够对键做一些推断,如:使用$ne和$push组,假设一个值不在数组里面就把他加进去,避免插入反复值
> db.papers.findOne()
{
"_id" : ObjectId("56aadaaa4e61b6d9f84d17e2"),
"title" : "People life",
"content" : "..."
}
>db.papers.update({"authorscited":{"$ne":"Richie"}},
...{"$push":{"authorscited":"Richie"}})
> db.papers.findOne()
{
"_id" : ObjectId("56aadaaa4e61b6d9f84d17e2"),
"title" : "People life",
"content" : "...",
"authors cited" : [
"Richie"
]
}
--再次增加相同元素,文档没有变化
> db.papers.update({"authorscited":{"$ne":"Richie"}},
...{"$push":{"authorscited":"Richie"}})
> db.papers.findOne()
{
"_id" : ObjectId("56aadaaa4e61b6d9f84d17e2"),
"title" : "People life",
"content" : "...",
"authors cited" : [
"Richie"
]
}
5. $addToSet
$addToSet作用:能够取代$ne和$push组全,在数组里增加一个元素且能增加多个元素,也能避免插入反复值
>db.users.findOne({"username":"licz"}
{
"_id" : ObjectId("56a8828b308203e00e436b01"),
"relationships" : {
"friends" : 43,
"enemies" : 5
},
"username" : "licz"
}
> db.users.update({"username":"licz"},{"$addToSet":{"email":"licz@163.com"}})
>db.users.findOne({"username":"licz"})
{
"_id" : ObjectId("56a8828b308203e00e436b01"),
"relationships" : {
"friends" : 43,
"enemies" : 5
},
"username" : "licz",
"email" : [
"licz@163.com"
]
}
--再运行增加数组元素
> db.users.update({"username":"licz"},{"$addToSet":{"email":"licz@qq.com"}})
> db.users.findOne({"username":"licz"})
{
"_id" : ObjectId("56a8828b308203e00e436b01"),
"relationships" : {
"friends" : 43,
"enemies" : 5
},
"username" : "licz",
"email" : [
"licz@163.com",
"licz@qq.com"
]
}
6. $each
$addToSet和$each组合,能够为数组增加多个不同的值
> db.users.update({"username":"licz"},
...{"$addToSet":{"email":{"$each":["licz@umessage.com","licz@sina.com"]}}})
>db.users.findOne({"username":"licz"})
{
"_id" : ObjectId("56a8828b308203e00e436b01"),
"relationships" : {
"friends" : 43,
"enemies" : 5
},
"username" : "licz",
"email" : [
"licz@163.com",
"licz@qq.com",
"licz@umessage.com",
"licz@sina.com"
]
}
7. $pop
$pop改动器能够从数组不论什么一端删除元素。
{$pop:{key:1}}从末尾端删除元素
{$pop:{key:-1}}从开头端删除元素
8. $pull
$pull能够基于特定条件来删除数组元素,而不仅仅是根据位置
>db.lists.insert({"todo":["dishs","laundry","drycleaning"]})
> db.lists.update({},{"$pull":{"todo":"laundry"}})
> db.lists.findOne()
{
"_id" : ObjectId("56aafd4a4e61b6d9f84d17e3"),
"todo" : [
"dishs",
"dry cleaning"
]
}
$pull会将全部匹配的部分删掉。对数组[1,1,2,1]运行pull 1,得到的结果是仅仅有一个元素[2]
数组的定位改动器
有两种方法操作数组中的值:通过位置和定位操作符$
数组都是以0开头的,能够直接用下标直接作为键来选择元素。例如以下
>db.blog.posts.findOne({"title" : "A blog post"})
{
"_id" : ObjectId("4b2d75476cc613d5ee930164"),
"title" : "A blog post",
"content" : "...",
"comments" : [
{
"name" :"joe",
"email" :"joe@example.com",
"content" :"nice post."
},
{
"name" :"bob",
"email" :"bob@example.com",
"content" :"good post."
}
]
}
>db.blog.posts.update({"title" : "A blogpost"},{"$inc":{"comments.0.visits" : 1}})
>db.blog.posts.findOne({"title" : "A blog post"})
{
"_id" : ObjectId("4b2d75476cc613d5ee930164"),
"title" : "A blog post",
"content" : "...",
"comments" : [
{
"name" :"joe",
"email" :"joe@example.com",
"content" :"nice post.",
"visits" : 1
},
{
"name" :"bob",
"email" :"bob@example.com",
"content" : "goodpost."
}
]
}
但非常多情况我们不知道要改动数组下标是多少,这时就能够使用定位操作符$,用来定位查询文档已经匹配的元素,并进行更新。
>db.blog.posts.update({"comments.name":"bob"},{"$set":{"comments.$.name":"licz"}})
>db.blog.posts.findOne({"title" : "A blog post"})
{
"_id" : ObjectId("4b2d75476cc613d5ee930164"),
"title" : "A blog post",
"content" : "...",
"comments" : [
{
"name" :"joe",
"email" :"joe@example.com",
"content" :"nice post.",
"visits" : 1
},
{
"name" :"licz",
"email" :"bob@example.com",
"content" :"good post."
}
]
}
upsert更新方法
upsert是一种特殊的更新。要是没有文档符合更新条件,就会以这个条件创建一个新文档,假设匹配就更新。
事实上就是update的第三參数。默认就是false.
> db.analytics.find()
{ "_id" :ObjectId("56a88706308203e00e436b04"), "url" :"www.baidu.com", "pageview" : 2, "visits" : 3 }
> db.analytics.update({"url":"www.csdn.net"},{"$inc":{"visits": 1}},true)
> db.analytics.find()
{ "_id" :ObjectId("56a88706308203e00e436b04"), "url" :"www.baidu.com", "pageview" : 2, "visits" : 3 }
{ "_id" :ObjectId("56ab094c638a1346c373d5d9"), "url" :"www.csdn.net", "visits" : 1 }
save 函数
save是一个shell函数,能够文档不存在时插入,存在时更新。它仅仅有一个參数:文档
使用例如以下:
> var x=db.foo.findOne()
> x.sum = 50
50
> db.foo.save(x)
> db.foo.find()
{ "_id" :ObjectId("56a88f55308203e00e436b07"), "count" :"1", "num" : 42, "sum" : 50 }
更新很多其它的文档
默认情况下,更新仅仅能对条件的第一个文档运行操作。
要使用全部文档都得到更新,能够设置update的第4个參数为ture,默认是false
比如:
给全部特定日期过生日的用户发一份礼物,就可使用多文档更新。将gift增加到他们的账号.
>db.users.update({"birthday":"1988/11/1"},{"$set":{gift:"Happybirthday!"}},false,true)
--查看更新了多少文档。n就是这个值
> db.runCommand({getLastError : 1})
{
"connectionId" : 13,
"n" : 3,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
}
MongoDB创建\更新\删除文档操作