E11000 duplicate key error index when updating document

2,687 views
Skip to first unread message

Kris

unread,
Jul 5, 2012, 5:47:21 AM7/5/12
to mongodb-user
My document from collection WatcherNotificationMongoDb looks like:
{
"_id" : ObjectId("4fc6c16a9c425bc0d944a609"),
"messageId" : "325401176381594ea655d483c905aaf789b38b167",
"watcherId" : NumberLong(325401176),
"brochureId" : NumberLong(327736393),
"recipientIdentifier" : "e36d546064d3253259cf862a75e0fd6a6150eb81",
"token" : "32540117694e801b27c254b69a3873a6e1f0ae2e2",
"messageType" : "WATCHER_MAIL",
"dateCreated" : ISODate("2012-05-31T00:55:06.702Z"),
"position" : 0,
"viewType" : "IMPLICIT",
"timeZoneId" : "Europe/Berlin",
"dateViewed" : ISODate("2012-06-20T06:32:55.497Z")
}

When I want update "dateViewed" and "viewType" I get this error:

com.mongodb.MongoException$DuplicateKey: E11000 duplicate key error
index: trafficStatistic.WatcherNotificationMongoDb.$_id_ dup key: { :
ObjectId('4fc6c16a9c425bc0d944a609') }
at com.mongodb.CommandResult.getException(CommandResult.java:
80)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:
116)
at
com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:126)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:148)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:132)
at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:
336)
at com.mongodb.DBCollection.save(DBCollection.java:632)

What can be wrong?

gregor

unread,
Jul 5, 2012, 8:03:06 AM7/5/12
to mongod...@googlegroups.com
Do you have unique indexes on those fields?

Stephen Steneker

unread,
Jul 5, 2012, 8:31:20 AM7/5/12
to mongod...@googlegroups.com
When I want update "dateViewed" and "viewType" I get this error:

com.mongodb.MongoException$DuplicateKey: E11000 duplicate key error
index: trafficStatistic.WatcherNotificationMongoDb.$_id_  dup key: { :
ObjectId('4fc6c16a9c425bc0d944a609') }

Hi Kris,

The error message has some helpful information .. you are trying to insert a document that would cause a duplicate key violation.

The specific collection and index is:
   trafficStatistic.WatcherNotificationMongoDb.$_id

The duplicate key value is:
  ObjectId('4fc6c16a9c425bc0d944a609')

It looks like perhaps your code is attempting an insert of the same document instead of an update.

Can you share the update statement in your code that triggers this error?

Cheers,
Stephen

Kris

unread,
Jul 16, 2012, 7:18:17 AM7/16/12
to mongod...@googlegroups.com
No I don't have any unique indexes on this collection.

Kris

unread,
Jul 16, 2012, 7:34:05 AM7/16/12
to mongod...@googlegroups.com
My code looks like:

final Map<String, String> query = ["brochureId": brochureId, "recipientIdentifier": recipientIdentifier]
        final List<WatcherNotificationMongoDb> watcherNotifications = WatcherNotificationMongoDb.findAll(query).toList()
        if (!watcherNotifications){
            return
        }

        for(final WatcherNotificationMongoDb notification : watcherNotifications) {
            if (notification.dateViewed){
                log.debug("...")
            } else {
                notification.dateViewed = new Date()
                notification.viewType = viewType
                if(!notification.save()) {
                    log.error("...")
                }
            }
        }

I found that update works because document in database have dateViewed set to this same time when this errors occur.
I get this problem also on other collection and I can't reproduce it locally (the problem appear on production).
Reply all
Reply to author
Forward
0 new messages