mongoDB操作方法

最近在弄MongoDB,总结了一下操作的方法, MongoDB 还是很方便的,写出来的PHP语句非常精简,感觉是直接对数据进行操作,是PDO都无法超越的。同时,可以随时更改的数据结构极大地方便了软件的初期开发。但是对软件的设计进行规划也是很有必要的。

 

 

Mongodb 如果存在则update如果不存在则insert的方法,使用 upsert 旗帜

例子:

db.inventory.update(
                     { type: "book", item : "journal" },
                     { $set : { qty: 10 } },
                     { upsert : true }
                   )

 

 

 

往集合批量添加元素需要使用$each

> db.xtxt.update({"asdf":11}, { $push : {"group":[5,4,6,8] } },{upsert: true} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.xtxt.find()
{ "_id" : ObjectId("53b2e2f39f1c90bdf306da98"), "asdf" : 11, "group" : [ { "wk" : "asdf", "ee" : 5 }, { "wk" : "asddf", "ee" : 25 }, [ 5, 4, 6, 8 ] ] }
{ "_id" : ObjectId("53b2db39c3a9fcd1793c9b1e"), "asdf" : 12, "group" : [ ] }
> db.xtxt.update({"asdf":11}, { $pop : {"group": 1 } } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.xtxt.find()
{ "_id" : ObjectId("53b2e2f39f1c90bdf306da98"), "asdf" : 11, "group" : [ { "wk" : "asdf", "ee" : 5 }, { "wk" : "asddf", "ee" : 25 } ] }
{ "_id" : ObjectId("53b2db39c3a9fcd1793c9b1e"), "asdf" : 12, "group" : [ ] }
> db.xtxt.update({"asdf":11}, { $push : {"group":{$each: [5,4,6,8] } } },{upsert: true} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.xtxt.find()
{ "_id" : ObjectId("53b2e2f39f1c90bdf306da98"), "asdf" : 11, "group" : [ { "wk" : "asdf", "ee" : 5 }, { "wk" : "asddf", "ee" : 25 }, 5, 4, 6, 8 ] }
{ "_id" : ObjectId("53b2db39c3a9fcd1793c9b1e"), "asdf" : 12, "group" : [ ] }
>

不过更方便的是使用 $pushAll

> db.xtxt.update({"asdf":11}, { $pushAll : {"group":[0,888,555,5,4,6,8] } },{upsert: true} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.xtxt.find()
{ "_id" : ObjectId("53b2e2f39f1c90bdf306da98"), "asdf" : 11, "group" : [ { "wk" : "asdf", "ee" : 5 }, { "wk" : "asddf", "ee" : 25 }, 5, 4, 6, 8, 0, 888, 555, 5, 4, 6, 8 ] }
{ "_id" : ObjectId("53b2db39c3a9fcd1793c9b1e"), "asdf" : 12, "group" : [ ] }
>

 

 

 

更新集合里面的内容可以使用$elemMatch ,并且配合 $

> db.xtxt.update( { "asdf":11, "group" : {$elemMatch : {"wk": "asdf"} } }, { $set : {"group.$.ee":100 }} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.xtxt.find()
{ "_id" : ObjectId("53b2e2f39f1c90bdf306da98"), "asdf" : 11, "group" : [ { "wk" : "asdf", "ee" : 100 }, { "wk" : "asddf", "ee" : 25 }, 5, 4, 6, 8, 0, 888, 555, 5, 4, 6, 8 ] }
{ "_id" : ObjectId("53b2db39c3a9fcd1793c9b1e"), "asdf" : 12, "group" : [ ] }
>

 

这篇博文发表在 开发运维 | Dev Ops 目录下,标签为 , , ,
如需引用,请使用链接:https://note.mc256.dev/?p=410

This article published in 开发运维 | Dev Ops with tags , , , .
Cite this page using this link:https://note.mc256.dev/?p=410

您的邮箱地址不会被公开,评论使用Gravatar头像。
Your email address will not be published. This blog is using Gravatar.

正在提交评论... Submitting ...
正在为您准备评论控件 Loading Comment Plugin
Copyright © 2013-2024 mc256. All Rights Reserved.
Powered by WordPress on top of a dual-stack k3s Cluster using JuiceFS.
Wordpress Theme Designed By mc256.
Encrypted By Let's Encrypt.  Hosted On Linode + OVH + AWS.
DNS Provided By Hostker.
Status Page by CloudFlare Worker.