Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Upsert-php mongodb-Can';不要插入新记录_Php_Mongodb_Upsert - Fatal编程技术网

Upsert-php mongodb-Can';不要插入新记录

Upsert-php mongodb-Can';不要插入新记录,php,mongodb,upsert,Php,Mongodb,Upsert,我正在运行以下代码。如果记录已经存在,它将在$updated\u fields\u数组中用新字段覆盖现有字段。但是如果记录不存在,我不知道如何添加一个带有新字段的新记录($new\u fields\u array)。我应该使用哪个接线员?有人能帮我吗 $current_time = time(); $current_mongo_date = new MongoDate($current_time); $collection = $this->mongo->s

我正在运行以下代码。如果记录已经存在,它将在$updated\u fields\u数组中用新字段覆盖现有字段。但是如果记录不存在,我不知道如何添加一个带有新字段的新记录($new\u fields\u 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运算符一起使用(请参阅)。请参阅下面的示例(注意,{email:1,name:1}上应该有唯一的索引)


埃夫根尼的答案是可以的,但你遗漏了一个问题

updated_fields_array
new_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
);

并更改查询(如Evgeny所写或


我不得不使用updateOne

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

@YannaGenkin它到底是如何工作的-没有插入文档操作系统某些字段丢失或错误?很遗憾,没有插入文档。您对集合是否有任何特定约束,例如唯一索引或文档验证,可能会阻止插入新文档?否,我在映射yml文件时没有这样的定义。您是否有权访问mongodb shell,在那里您可以使用
db.AccountSettings.getIndexes()
检查索引,请参阅使用
db.getcollectionfos()
的集合约束,并尝试使用
db.AccountSettings.update
$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)
);
$result = $collection->updateOne(
        array('first_id' => 123456,'some_number' => 333333),
        array('$set' => array('some_status' => 'Delayed')),
        array('upsert' => true)
);