Mongo数据库更新

时间:2017-04-15 03:55:43

标签: mongodb mongodb-query

我是mongo的新手,我正在寻求帮助。

MongoDB Enterprise > db.servmon.find({ "hostName" : “server-prf-004”})   
{ "_id" : ObjectId("58f0a4a2ff980d97cce0a79c"), "hostName" : “server-prf-004", "thresholds" : [ { "warn" : "None", "crit" : "None", "contact_mail" : "None", "type" : "loadcheck", "contact_page" : "None" }, { "warn" : "None", "crit" : "None", "contact_mail" : "None", "type" : "mu_check", "contact_page" : "None" }, { "map" : "/ora,/arch", "crit" : "None", "warn" : "None", "contact_mail" : "None", "type" : "diskmon", "contact_page" : "None" } ] }

如果我只想更改数据库中的一个条目

{ "warn" : "None", "crit" : "None", "contact_mail" : "None", "type" : "loadcheck", "contact_page" : "None” } to  to { "warn" : “90", "crit" : “80", "contact_mail" : "None", "type" : "loadcheck", "contact_page" : "None” }

什么是最佳选择?

我尝试使用update和findModify,但它正在更新阈值后的所有条目,如下所示:

MongoDB Enterprise > db.servmon.findAndModify ( { query:{"hostName" : “server-prf-004","thresholds" : { "warn" : "None", "crit" : "None", "contact_mail" : "None", "type" : "mu_check", "contact_page" : "None" } }, update :{ "hostName" : "server-prf-002","thresholds" : { "warn" : "80", "crit" : "90", "contact_mail" : "None", "type" : "mu_check", "contact_page" : "None" }  } ,upsert: true } )

MongoDB Enterprise > db.servmon.find({ "hostName" : “server-prf-004"})
{ "_id" : ObjectId("58f0a4a2ff980d97cce0a799"), "hostName" : "server-prf-001", "thresholds" : { "warn" : "80", "crit" : "90", "contact_mail" : "None", "type" : "mu_check", "contact_page" : "None" } }
MongoDB Enterprise >

2 个答案:

答案 0 :(得分:0)

  

执行更新   所选文件。更新字段使用相同的更新   运算符或字段:修改所选值的规范   文档。

     

update()方法修改现有的特定字段   完整地记录或替换现有文件。

您正在使用更新版本来完全替换文档。

您需要的是更新变体,它使用update运算符有选择地更新字段。

这样的东西

db.servmon.findAndModify ( { query:{"hostName" : "server-prf-004", "thresholds" : { "warn" : "None", "crit" : "None", "contact_mail" : "None", "type" : "mu_check", "contact_page" : "None" } }, update :{ $set:{ "hostName" : "server-prf-002", "thresholds.$" : { "warn" : "80", "crit" : "90", "contact_mail" : "None", "type" : "mu_check", "contact_page" : "None" } } } ,upsert: true } )

注意:$set是一个更新运算符,用于更新hostnamethresholds.$,它使用位置运算符将所选数组从查询更新为更新值。

https://docs.mongodb.com/manual/reference/command/findAndModify/#dbcmd.findAndModify

https://docs.mongodb.com/manual/reference/method/db.collection.update/#update-parameter

https://docs.mongodb.com/manual/reference/operator/update/set/#up._S_set

https://docs.mongodb.com/manual/reference/operator/update/positional/

答案 1 :(得分:0)

如果我理解正确,即使您的查询产生一个或多个结果,您也只想更新一个文档。你可以试试这个。使用findOne查询返回唯一的_id。然后在更新字段中使用该_id。这是一次操作。

db.update({_id:
                db.servmon.findOne(
                    {
                        "hostName" : “server-prf-004",
                        "thresholds" : { "warn" : "None", "crit" : "None", "contact_mail" : "None", "type" : "mu_check", "contact_page" : "None" } 
                    })['_id']
            }
            {
                $set:
                    {
                         "hostName" : "server-prf-002","thresholds" : { "warn" : "80", "crit" : "90", "contact_mail" : "None", "type" : "mu_check", "contact_page" : "None" }


                    }
            },
            {upsert:true})