好吧,这似乎我可能需要做多次mongodb搜索和插入,但我想我会问。
我有一个问题,我们需要在数据库中搜索商业名称,如果它不在数据库中然后添加它,但如果它在数据库中,那么我需要它来查看它是否由同一用户拥有ID。如果没有返回"sorry we can not add this to your account."
后跟正确所有者的用户ID。
$cursor = $collection->find(array("businessname" => $businessname));
会起作用但这意味着我需要做以下
$collection = $this->db->retail_details;
$cursor = $collection->find(array("businessname" => $businessname));
if ($cursor->count() > 0)
{
SEARCH FOR OWNER
if(OWNER != CURRENTUSER)
{
return "Sorry You can not make changes please contact (OWNER)"
}
}
else{
INSERT NEW LEAD
}
虽然我知道这会起作用,但我发现它可能会变得混乱。 作为经理的用户需要能够查看和编辑任何潜在客户。
答案 0 :(得分:0)
您应该可以通过将businessname和所有者作为标准进行匹配,将其作为upsert执行此操作。
Upsert:如果没有文档符合$ criteria,则会出现一个新文档 从$ criteria和$ new_object创建
将遵循:
<?php
$collection = $this->db->retail_details;
try {
$collection->update(
array("businessname" => $businessname, "owner" => $currentuser),
$new_lead, // new lead document to insert
array("upsert" => true, "safe" => true)
);
} catch (Exception $e) {
// Something went wrong ..
}
?>