一、插入\创建文档


--当插入一个不存在的文档时,会自动创建一个文档

[[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" : "...",

       "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":"[email protected]"}})

>db.users.findOne({"username":"licz"})

{

       "_id" : ObjectId("56a8828b308203e00e436b01"),

       "relationships" : {

                "friends" : 43,

                "enemies" : 5

       },

       "username" : "licz",

       "email" : [

                "[email protected]"

       ]

}

 

--再执行增加数组元素

> db.users.update({"username":"licz"},{"$addToSet":{"email":"[email protected]"}})

> db.users.findOne({"username":"licz"})

{

       "_id" : ObjectId("56a8828b308203e00e436b01"),

       "relationships" : {

                "friends" : 43,

                "enemies" : 5

       },

       "username" : "licz",

       "email" : [

                "[email protected]",

               "[email protected]"

       ]

}

 

6. $each

$addToSet$each组合,可以为数组添加多个不同的值

> db.users.update({"username":"licz"},

...{"$addToSet":{"email":{"$each":["[email protected]","[email protected]"]}}})

>db.users.findOne({"username":"licz"})

{

       "_id" : ObjectId("56a8828b308203e00e436b01"),

       "relationships" : {

                "friends" : 43,

                "enemies" : 5

       },

       "username" : "licz",

       "email" : [

                "[email protected]",

                "[email protected]",

                "[email protected]",

                "[email protected]"

       ]

}

 

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" :"[email protected]",

                        "content" :"nice post."

                },

                {

                        "name" :"bob",

                        "email" :"[email protected]",

                        "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" :"[email protected]",

                        "content" :"nice post.",

                        "visits" : 1

                },

                {

                        "name" :"bob",

                        "email" :"[email protected]",

                        "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" :"[email protected]",

                        "content" :"nice post.",

                        "visits" : 1

                },

                {

                        "name" :"licz",

                        "email" :"[email protected]",

                        "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

}

 

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

python连接mongodb操作代码详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。