PHP MongoDB 用一个查询更新多个字段

发布于 2024-12-09 04:42:19 字数 345 浏览 4 评论 0原文

我尝试运行此查询:

$collection->update(
    array('_id' => 'mongoIDhere'),
    array(
        '$set' => array("parent" => 'data'),
        array("parents" => 'data')
    ),
    array("upsert" => true)
);

但它只会更新第二个“set”参数(即 array("parents" => 'data') )。当在两个单独的查询中完成时,这些工作正常,但放在一起就不行了 - 这是怎么回事?

I've tried running this query:

$collection->update(
    array('_id' => 'mongoIDhere'),
    array(
        '$set' => array("parent" => 'data'),
        array("parents" => 'data')
    ),
    array("upsert" => true)
);

But it will only update the second "set" parameter (which is array("parents" => 'data') ). When done in two separate queries these work fine, but together it doesn't - what gives?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑看君怀她人 2024-12-16 04:42:19
$collection->update(
    array('_id' => 'mongoIDhere'),
    array(
        '$set' => array("parent" => 'data'),
    ),
    array("upsert" => true)
);

请记住,MongoDB 只接受键->值对格式的数组,即 array("parents" => 'data') 应该是 $something => array("parents" => 'data') 或在 php.ini 文件中进行更改,以便允许空值作为键。

$collection->update(
    array('_id' => 'mongoIDhere'),
    array(
        '$set' => array("parent" => 'data'),
    ),
    array("upsert" => true)
);

Remember MongoDB only accepts array in format of key->value pair i.e. array("parents" => 'data') should be $something => array("parents" => 'data') OR make changed in your php.ini file so that it will be allow null values as key.

兮子 2024-12-16 04:42:19

尝试使用多个选项

$collection->update(
    array('_id' => 'mongoIDhere'),
    array('$set' => array("parent" => 'data')),
    array("upsert" => true, "multiple" => true)
);

“多个”选项

所有匹配$criteria的文档都将被更新。 MongoCollection::update() 的行为与 MongoCollection::remove() 完全相反:它默认更新一个文档,而不是所有匹配的文档。建议您始终指定是要更新多个文档还是单个文档,因为数据库可能会在将来的某个时刻更改其默认行为。

PHP 文档中的 Mongocollection

Try with multiple option

$collection->update(
    array('_id' => 'mongoIDhere'),
    array('$set' => array("parent" => 'data')),
    array("upsert" => true, "multiple" => true)
);

"multiple" option

All documents matching $criteria will be updated. MongoCollection::update() has exactly the opposite behavior of MongoCollection::remove(): it updates one document by default, not all matching documents. It is recommended that you always specify whether you want to update multiple documents or a single document, as the database may change its default behavior at some point in the future.

Mongocollection in PHP Doc's

二智少女 2024-12-16 04:42:19

尝试这样的事情。

$collection->update(
            array('_id' => 'mongoIDhere'),
            array(
                '$set' => 
                          array(
                                 array("parent" => 'data'),
                                 array("parents" => 'data')
                               )
            ),
            array("upsert" => true) 
         );

希望这会起作用..

Try something like this.

$collection->update(
            array('_id' => 'mongoIDhere'),
            array(
                '$set' => 
                          array(
                                 array("parent" => 'data'),
                                 array("parents" => 'data')
                               )
            ),
            array("upsert" => true) 
         );

Hope this will work..

疾风者 2024-12-16 04:42:19

假设您正在使用 https://github.com/mongodb/mongo-php-library 你应该尝试这个:

$collection->update(
  ['_id' => 'mongoIDhere],
  ['$set' => ['parent' => 'data', 'parent2' => 'data2']],
  ['upsert' => true]
);

我希望你在提出问题后找到了解决问题的方法。然而,我今天遇到了同样的问题,无法通过搜索引擎找到任何答案,所以我认为这可能会帮助其他人。

Assuming that you are using https://github.com/mongodb/mongo-php-library you should try this:

$collection->update(
  ['_id' => 'mongoIDhere],
  ['$set' => ['parent' => 'data', 'parent2' => 'data2']],
  ['upsert' => true]
);

I hope that you found a way to fix your issue since you asked the question. However I faced same issue today and couldn't find any answer with search engines so I though that this might help other fellas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文