MongoDB创建更新删除文档操作
Posted 常飞梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB创建更新删除文档操作相关的知识,希望对你有一定的参考价值。
一、插入\创建文档
--当插入一个不存在的文档时,会自动创建一个文档
[[email protected] ~]# 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":"[email protected]","content":"goodpost!"}}})
>db.blog.posts.findOne({"title":"A Oracle error summary"})
{
"_id" : ObjectId("56aad2744e61b6d9f84d17e1"),
"title" : "A Oracle error summary",
"content" :"...",
"comments" : [
{
"name" :"licz",
"email" :"[email protected]",
"content" :"good post!"
}
]
}
--再次加一个数据元素
... {"$push":{"comments":{"name":"haley","email":"[email protected]","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" :"[email protected]",
"content" :"good post!"
},
{
"name" :"haley",
"email" :"[email protected]",
"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" : "...",
以上是关于MongoDB创建更新删除文档操作的主要内容,如果未能解决你的问题,请参考以下文章