好吧,我可能需要进行多个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
}
虽然我知道这会奏效,但我发现它可能会变得一团糟。作为管理者的用户需要能够查看和编辑任何潜在客户。
您应该能够通过匹配企业名称和所有者作为标准,将其作为追加销售来完成。
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 ..
}
?>