简体   繁体   English

MongoDB Perl更新命令

[英]MongoDB Perl update command

I would like to update a field without losing the others fields of the collection from perl script. 我想更新一个字段而不会丢失perl脚本中集合的其他字段。

for example I have this collection : 例如我有这个集合:

{
"userid": 1234,
"username": "tester",
"password": "*****",
"fullname": "For Test Only",
"emails" :[
     { "email": "test1@mail.com", "active": true },
     { "email": "test2@mail.com", "active": true }
]
}

And I would like to change to this : 我想更改为:

{
 "userid": 1234
 "username": "tester",
 "password": "*****",
 "fullname": "For Test Only",
 "emails" :[
     { "email": "test1@mail.com", "active": false},
     { "email": "test2@mail.com", "active": true }
]
}

When I tried to change it from my perl script using this : 当我尝试使用以下命令从我的perl脚本中更改它时:

$update_result = $users->update_one(
                                    { 'userid' => $doc->{'userid'}},
                                    { '$set'     => { 'emails.[0].email.active' => false }},
                                    { 'upsert'   => 1}
                                );

I got the following result which is NOT expected : 我得到了以下预期不到的结果:

{
     "userid": 1234
     "username": "tester",
     "password": "*****",
     "fullname": "For Test Only",
     "emails" :[
         { "active": false},
         { "email": "test2@mail.com", "active": true }
    ]
}

The email disappear !! 电子邮件消失了! . Any idea why is NOT keeping the email ? 知道为什么不保留电子邮件吗?

Thank you. 谢谢。

you don't need the extra email. 您不需要额外的email. in there: 在那里:

$update_result = $users->update_one(
                                    { 'userid' => $doc->{'userid'}},
                                    { '$set'     => { 'emails.[0].active' => false }},
                                    { 'upsert'   => 1}
                                );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM