【发布时间】:2016-07-02 05:19:33
【问题描述】:
我正在运行以下代码。如果记录已经存在,它会用 $updated_fields_array 处的新字段覆盖现有字段。但是,如果记录不存在,我不知道如何使用新字段($new_fields_array)添加新记录。我应该使用哪个运算符?有人可以帮我吗?
$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;
【问题讨论】: