PHP MongoDB 用一个查询更新多个字段
我尝试运行此查询:
$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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请记住,MongoDB 只接受键->值对格式的数组,即
array("parents" => 'data')
应该是$something => array("parents" => 'data')
或在 php.ini 文件中进行更改,以便允许空值作为键。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.尝试使用多个选项
“多个”选项
所有匹配$criteria的文档都将被更新。 MongoCollection::update() 的行为与 MongoCollection::remove() 完全相反:它默认更新一个文档,而不是所有匹配的文档。建议您始终指定是要更新多个文档还是单个文档,因为数据库可能会在将来的某个时刻更改其默认行为。
PHP 文档中的 Mongocollection
Try with multiple option
"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
尝试这样的事情。
希望这会起作用..
Try something like this.
Hope this will work..
假设您正在使用 https://github.com/mongodb/mongo-php-library 你应该尝试这个:
我希望你在提出问题后找到了解决问题的方法。然而,我今天遇到了同样的问题,无法通过搜索引擎找到任何答案,所以我认为这可能会帮助其他人。
Assuming that you are using https://github.com/mongodb/mongo-php-library you should try this:
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.