我正在運行以下代碼。如果記錄已經存在,它將使用新的$ updated_fields_array覆蓋現有的字段。但是,如果記錄不存在,我不知道如何添加新記錄與新字段($ new_fields_array)。我應該使用哪個操作符?有人可以幫我嗎?Upsert - php mongodb-無法插入新記錄
$current_time = time();
$current_mongo_date = new MongoDate($current_time);
$collection = $this->mongo->selectCollection(MONGO_NOTIFY_DB, 'AccountSettings');
$criteria_array=array("email" => $email, "name" => $name);
$updated_fields_array = array (
'last_sent' => $current_time,
'updated' => $current_mongo_date
);
$new_fields_array = array (
'created' => $current_mongo_date,
'updated' => $current_mongo_date,
'email' => $email,
'name' => $name,
'last_sent' => $current_time,
'deleted' => FALSE
);
try {
$collection->update(
$criteria_array,
array('$set' => $updated_fields_array),
array("upsert" => true)
);
} catch (MongoCursorException $mce) {
log_message('error', 'QUERY UPDATE FAILED :: AccountSettings :: ' . print_r($updated_fields_array, true) . ' :: ' . $mce->getMessage());
}
return;
我試過了。它不起作用。 –
@YannaGenkin它究竟如何不起作用 - 沒有插入文檔os某些字段丟失或錯誤? –
不幸的是,沒有插入文檔。 –