Upsert - 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;

您可以将 UPDATE 命令与 upsert 选项和 $set/$setOnInsert 运算符一起使用(参见 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;

Evgeny 的答案不错,但你漏掉了一个问题

您在 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
);

并更改查询(如 Evgeny 所写或 Documentation

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

我不得不使用 updateOne

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