更新插入 - php mongodb - 无法插入新记录


Upsert - php mongodb- Can't insert a new record

我正在运行以下代码。 如果记录已存在,它将用 $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;

叶甫根尼的答案没问题,但你错过了一个问题

您在updated_fields_arraynew_fields_array中有重复的键,这会引发MongoDB WriteException,因为 Mongo 将首先创建新对象,然后才更新它。

所以改变

$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
);

$updated_fields_array = array (
    'last_sent' => $current_time,
    'updated' => $current_mongo_date
);
$new_fields_array = array (
    'created' => $current_mongo_date,
    'email' => $email,
    'name' => $name,
    'deleted' => FALSE
);

并更改查询(如叶夫根尼或文档所写)

$collection->update(
        $criteria_array,
        array('$set' => $updated_fields_array, '$setOnInsert'=> $new_fields_array),
        array("upsert" => true)
);

您可以使用带有 upsert 选项和 $set/$setOnInsert 运算符的 UPDATE 命令(请参阅 https://docs.mongodb.org/manual/reference/operator/update/setOnInsert/)。请参阅下面的示例(注意,您应该在 {email:1,name:1} 上具有唯一索引)

 $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,
    'deleted' => FALSE
);
try {
    $collection->update(
        $criteria_array,
        array('$set' => $updated_fields_array, '$setOnInsert'=> $new_fields_array),
        array("upsert" => true)
    );
} catch (MongoCursorException $mce) {
    log_message('error', 'QUERY UPDATE FAILED :: AccountSettings :: ' . print_r($updated_fields_array, true) . ' :: ' . $mce->getMessage());
}
return;

我不得不使用 updateOne

$result = $collection->updateOne(
        array('first_id' => 123456,'some_number' => 333333),
        array('$set' => array('some_status' => 'Delayed')),
        array('upsert' => true)
);