mongodb update if a field does not exist

与世无争的帅哥 提交于 2019-12-11 05:55:20

问题


$db->akis->update(
array("h" => (string) $_SESSION["_id"], "m" => array('$exists' => false)),
array('$set' => array("k" => $name)),
array("multiple" => true)
);

what i did in here is, if there is an m field, do not update k. What I want to add is, "if m field exists" update i instead of k field, how can I manage this ?

thank you


回答1:


I think you will need to do two separate queries here. That is just too conditional for MongoDB query parser to handle.

So you will need to put your logic into two separate queries with the second looking like:

$db->akis->update(
array("h" => (string) $_SESSION["_id"], "m" => array('$exists' => true)),
array('$set' => array("i" => $name)),
array("multiple" => true)
);

Running one after the other.



来源:https://stackoverflow.com/questions/12706530/mongodb-update-if-a-field-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!