Gab*_*iel 5 mongodb nosql mongodb-php
我希望在已存在的情况下替换文档,如果不存在,我希望将其插入.我怎么能在mongoDb中做到这一点?
我需要这样的东西,但在一个查询中:
find by a "where statement"
if exists, replace whole document
else, insert
Run Code Online (Sandbox Code Playgroud)
谢谢!
使用集合更新。
在下面的示例中,第一次更新调用将“插入或替换”文档(包括查询中的名称字段)。在第二个更新调用中,将插入文档或仅更新 Joe 的工作,而使文档的其余部分保持不变。区别在于“$set”操作。
<?php
$c->update(
array("name" => "joe"),
array("username" => "joe312", "job" => "Codemonkey"),
array("upsert" => true));
$c->update(
array("name" => "joe"),
array("$set" => array("job" => "Bartender")),
array("upsert" => true));
?>
Run Code Online (Sandbox Code Playgroud)