E11000 duplicate key error - want to ignore duplicate keys with PHP driver

3,899 views
Skip to first unread message

KingCanadian

unread,
Mar 29, 2012, 3:11:49 AM3/29/12
to mongodb-user
Good morning!

I inserted a lot of data into my collection using my own id as _id.
Now i want to insert again - but only that data which does not exist
in the collection.
So inserts with duplicat keys should be ignored, but i get an
exception:
E11000 duplicate key error

Here is my code:

try
{
$coll_mmemb2->insert($daten_mmemb2, array('safe'=>true,
'ContinueOnError'=>true));
}
catch(MongoCursorException $e)
{
die($e);
}

I read that mongodb should ignore duplicate keys by default. Also
there is a flag
ContinueOnError - but using it does not work with the PHP driver and i
want only
to Continue when the Error is duplicate key and stop if it is an other
error.

Regards,

Juergen

Sam Millman

unread,
Mar 29, 2012, 4:10:12 AM3/29/12
to mongod...@googlegroups.com
"I read that mongodb should ignore duplicate keys by default."

In What way can it ignore?


"ContinueOnError - but using it does not work with the PHP driver and i
want only"

That sounds really dodgy and not sure why you would want to continue on a error.

Also safe insert:

"Can be a boolean or integer, defaults to FALSE. If FALSE, the program continues executing without waiting for a database response. If TRUE, the program will wait for the database response and throw a MongoCursorException if the insert did not succeed. "

Since you are trying to insert on a duplicate key mongo will throw an error and since you are die()ing the app it will fail no matter what.

You could instead try http://www.php.net/manual/en/mongocollection.save.php which will upsert if the document does not exist which means it will still catch errors but will not die on duplicate key errors.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Sam Millman

unread,
Mar 29, 2012, 4:14:37 AM3/29/12
to mongod...@googlegroups.com
Also the continue on error only exists on batch ops: http://www.php.net/manual/en/mongocollection.batchinsert.php

Which makes sense, though still a dodgy thing to set.

KingCanadian

unread,
Mar 29, 2012, 4:49:50 AM3/29/12
to mongodb-user
> "I read that mongodb should ignore duplicate keys by default."
>
> In What way can it ignore?

There are a lot of issues for duplicate key errors - e.g.
https://jira.mongodb.org/browse/SERVER-509
This issue is fixed and solved - so i expect ist works in the actual
release.

> That sounds really dodgy and not sure why you would want to continue on a
> error.

I only want to continue on duplicate key error - on all other errors
the script should stop.

> You could instead tryhttp://www.php.net/manual/en/mongocollection.save.phpwhichwill upsert
> if the document does not exist which means it will still
> catch errors but will not die on duplicate key errors.

I dont want to do an update on the duplicated data - i should simply
be ignored. So upsert is not the right thing.

Sam Millman

unread,
Mar 29, 2012, 5:53:14 AM3/29/12
to mongod...@googlegroups.com
That JIRA relates to the http://www.php.net/manual/en/mongocollection.batchinsert.php fix in 1.2.7


"I dont want to do an update on the duplicated data - i should simply
be ignored. So upsert is not the right thing."

In which case you need to do an update like so:

$db->update(array( '_id' => new MongoId($someid) ), array($doc), array('upsert' => true, 'safe' => true);

That will ignore duplicate keys entirely.

Sam Millman

unread,
Mar 29, 2012, 5:54:26 AM3/29/12
to mongod...@googlegroups.com
Or at least I think that relates to that JIRA since dup keys can be skipped with that update above

Kristina Chodorow

unread,
Mar 29, 2012, 10:26:17 AM3/29/12
to mongod...@googlegroups.com
You have to use batchInsert to use the continueOnError case.  And you have to use continueOnError, not ContinueOnError.
To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.

Derick Rethans

unread,
Mar 29, 2012, 11:01:32 AM3/29/12
to mongodb-user
On Thu, 29 Mar 2012, KingCanadian wrote:

> try
> {
> $coll_mmemb2->insert($daten_mmemb2, array('safe'=>true,
> 'ContinueOnError'=>true));
> }
> catch(MongoCursorException $e)
> {
> die($e);
> }

Just don't add the "die". Without the "die", the script will happily
continue as you did succesfully catch and handle the exception.

continueOnError is only a flag for batchInsert() as Sam said.

cheers,
Derick

--
http://mongodb.org | http://derickrethans.nl
twitter: @derickr and @mongodb

Sam Millman

unread,
Mar 29, 2012, 11:19:47 AM3/29/12
to mongod...@googlegroups.com
After seeing this article come back I realised that even the upsert param in update actually just updates if its there (dunno how I forgot that) I mean you could just lose the die() (as Derick said) but then I suppose you would have to put a function there to check for when it isnt a dup key error and handle that error.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages