I have this query, but still get E11000 duplicate key error collection
error
$collection->insertMany($allData,['ordered' => false]);
MongoDB: 4.4, php driver version: 1.12.0
—————————–Update——————————
According to dododo’s answer (and comments), all documents will be inserted successfully, but you will get the duplicate error at the end.
In other words, this code will insert all documents without killing the app:
try{
$collection->insertMany($allData,['ordered' => false]);
}catch(Exception $e){
echo "Some documents are duplilcate but everthing ok.."
}
2
Answers
The error is quite clear.
Between the array $allData and the table contents you have duplicate keys.
CASUS: Empty Table
So let’s say your database table is:
where
_id
is your primary key in the database.And your $allData is
As you can see, the primary key insert data has duplicate values, the value 1 appears twice.
This gives then an error.
The same can occur if a database column is marked with an
UNIQUE INDEX
forcing values to be unique.Dump the data that’s present in $allData, study the contents, and remove the duplicates.
Casus: Filled table
And your $allData is
What happens is, it tries to insert a row into the table with primary key 1. The database engine checks for existing primary keys with that value. It finds one present, as there is already a row with primary key one. Then it throws an error, wich gets caught by php, which then throws it’s own exception which ends up with you. Duplicate key found.
In that case use upsert your query with the setOnInsert modifier for upsert. Something like:
This is expected behavior that mentioned in the doc here. Also, it’s not php related, same you can see in the shell:
which fails with:
result:
pay attention that there are 2 inserted documents, first attempt of
{ _id: 1 }
and{ _id : 2 }
.