mongo db用另一个jsondata更新一个字段

Sus*_*ush 1 mongodb

我有一个mongo db表

{
    "_id": ObjectId("531963b60748a6078fe4f345"),
    "acces": "172.1.6.2.18",
    "adapter": "Win 9",
    "flavour": "LokiSnap",
    "jobid": "8",
    "os": "VM-WIN7-32",
    "results": "",
    "tests": "Test01"
}
Run Code Online (Sandbox Code Playgroud)

这里的result领域是"".如何更新此表中的值result{"test" : "conenct","os":"osf"}基于jobid

更新表后是

{
"_id": ObjectId("531963b60748a6078fe4f345"),
"acces": "172.1.6.2.18",
"adapter": "Win 9",
"flavour": "LokiSnap",
"jobid": "8",
"os": "VM-WIN7-32",
"results": {
    "test": "conenct",
    "os": "osf"
}`,
"tests": "Test01"
Run Code Online (Sandbox Code Playgroud)

}

Har*_*rot 5

是的,使用$ set,这样就不会破坏整个文档,只需设置字段/字段

这是更新一个文档的查询

db.coll.update(
    {"jobid": "8"}, // the filter based on jobid
    {
        $set: { "results": { "test": "conenct", "os": "osf" } }
    } 
)
Run Code Online (Sandbox Code Playgroud)

这是更新所有文档的查询

db.coll.update(
    {"jobid": "8"}, // the filter based on jobid
    {
        $set: { "results": { "test": "conenct", "os": "osf" } }
    } ,
   { multi: true }
)
Run Code Online (Sandbox Code Playgroud)

注意: - 这里,"coll"是您的集合名称.