好吧,這個問題推動了我,我認爲_id是爲了在第一次插入它時使用ObjectID,當我嘗試使用_id進行更新時,它不正確。mongoDB不會允許我更新
這裏是我的代碼
//Save Data
function savedata($data){
$collection = $this->db->retail_logs;
$this->data = $data;
if($this->data['_id'] == NULL || $this->data['_id'] == "")
{
$this->data['_id'] = new MongoId();
}
else
{
$this->data['_id'] = ObjectID($this->data['_id']);
}
try {
$collection->update(
array("_id"=>$this->data['_id']),
$this->data, // new lead document to insert
array("upsert" => true, "safe" => true)
);
print $this->data['_id'];
} catch (Exception $e) {
print "we are not able to update";
}
}
我試圖做followinf
if($this->data['_id'] == NULL || $this->data['_id'] == "")
{
$this->data['_id'] = new MongoId();
}
else
{
$this->data['_id'] = ObjectID($this->data['_id']);
}
,但似乎沒有幫助。
正在發生的事情是正確地插在第一時間與對象ID(IDNumber中) 然後它更新時,刪除對象ID(),並插入用相同的IDNumber一個新的鉛作爲
所以看起來前像「的ID號」
什麼是'的print_r($ E)'輸出? – 2012-07-26 03:16:10
它更新,但它插入一個新的蒙戈行 – RussellHarrower 2012-07-26 03:23:42