google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

E11000 duplicate key error index on _id field #414

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I took some document from MongoDB, changed it and then saved it. For this I 
used "save" method from DatastoreImpl. After that I got this error. 

com.mongodb.MongoException$DuplicateKey: E11000 duplicate key error index: 
trafficStatistic.WatcherNotificationMongoDb.$_id_  dup key: { : 
ObjectId('4fc6c16a9c425bc0d944a609') }
        at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:764)
        at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:826)
        at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:820)
        at com.google.code.morphia.Datastore$save.call(Unknown Source)
        at com.google.code.morphia.Datastore$save.call(Unknown Source)

Is this a bug or should I use "update" method for updating documents.

Used versions:
MongoDB: 1.8.5
Morphia: morphia-1.00-SNAPSHOT

Original issue reported on code.google.com by wolw...@gmail.com on 25 Jun 2012 at 11:45

GoogleCodeExporter commented 9 years ago
The save operation also works for updating objects. It looks like you updated 
an embedded object with an id which already exists in the database. You cannot 
have two objects which have an embedded object with the same id.

Original comment by seb.schr...@gmail.com on 3 Jul 2012 at 9:53

GoogleCodeExporter commented 9 years ago
This document don't have embedded objects:
{
    "_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")
}

Original comment by wolw...@gmail.com on 5 Jul 2012 at 9:05

GoogleCodeExporter commented 9 years ago
I found that update works with E11000 error. So two questions are open:
1. Why this error occur.
2. Why documents are updated when this error occur.

I get this problem also on other collection.

Original comment by wolw...@gmail.com on 6 Jul 2012 at 8:38

GoogleCodeExporter commented 9 years ago
I sadly cannot reproduce your error using your version of mongodb and morphia. 
I can retrieve a document from mongodb, change its contents and save the 
updated document just fine. The save method does an update and inserts a new 
document if it does not exist.
Can you post the code where you change your document and save it?

Original comment by sebastia...@otto.de on 8 Jul 2012 at 9:04

GoogleCodeExporter commented 9 years ago
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("...")
                }
            }
        }

Original comment by wolw...@gmail.com on 16 Jul 2012 at 11:50

GoogleCodeExporter commented 9 years ago
I also can't reproduce it locally, the problem appear on production.

Original comment by wolw...@gmail.com on 16 Jul 2012 at 11:52