本文介绍了如果字段不存在,则 mongodb 更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在这里所做的是,如果有 m 字段,则不要更新 k.我想补充的是,如果 m 字段存在"更新 i 而不是 k 字段,我该如何管理?

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 ?

谢谢

推荐答案

我认为您需要在这里进行两个单独的查询.这对于 MongoDB 查询解析器来说太有条件了.

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)
);

一个接一个地运行.

这篇关于如果字段不存在,则 mongodb 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:12