Код работает нормально с первого раза. Я запускаю его (он вставляет документы), но при втором запуске выдает следующую ошибку:
Код: Выделить всё
Fatal error: Uncaught MongoDB\Driver\Exception\BulkWriteException: E11000 duplicate key error collection: accounts.posts index: postid dup key: { id: "2338...
Это мой код:
Код: Выделить всё
// I prepare the array $post_operations with all updateOne operations
// where $data is an object that contains all the document elements I want to insert
$posts_operations = array();
foreach ($this->posts as $id => $data) {
array_push($posts_operations, array('updateOne' => [['id' => $id], ['$set' => $data], ['upsert' => true]]));
}
// Then I execute the method bulkWrite to run all the updateOne operations
$insertPosts = $account_posts->bulkWrite($posts_operations);
Я иметь уникальный индекс, настроенный в коллекции для 'id'.
Большое спасибо за вашу помощь.
Подробнее здесь: https://stackoverflow.com/questions/630 ... es-duplica