Would I expect to get MongoError: E11000 duplicate key error index when using collection.save()

406 views
Skip to first unread message

Mooky

unread,
Nov 2, 2012, 6:00:40 AM11/2/12
to node-mongodb-native

I am saving a document thus:

resultsCollection.save(results, {safe: true}, function(err){
....
})

I am getting errors:
MongoError: E11000 duplicate key error index: DB.resultsCollection.
$_id_ dup key: { : ObjectId('50923b9de4b0b8b0e22c3d07') }

Should I get this error when using save() ?

Cheers.

Mooky

unread,
Nov 2, 2012, 6:02:35 AM11/2/12
to node-mongodb-native
Using "version": "1.0.2" of the driver ...

Christian Kvalheim

unread,
Nov 2, 2012, 6:16:42 AM11/2/12
to node-mong...@googlegroups.com
upgrade to latest then try again. you should avoid using save and do partial updates or inserts depending on your need as you'll avoid problems such a two processes changing the same whole document and loosing changes.

Mooky

unread,
Nov 2, 2012, 6:25:56 AM11/2/12
to node-mongodb-native
We have had an issue upgrading to the latest for some time - a
connection-related issue we havent been able to reproduce in a limited
example... (if you recall?)

With regard to using collection.save() - it is intended/desired that
the operation is idempotent.
If 2 processes happen to be trying the save the same doc, we are happy
for both operations to succeed because we know the doc in both cases
will be identical. We dont intend for 2 processes to be writing the
same doc - but if it happens due to some race condition, we dont mind
& we dont want 10's of 1000's of errors being logged...

So, was there a known issue with save()?



On Nov 2, 10:16 am, Christian Kvalheim <chris...@gmail.com> wrote:
> upgrade to latest then try again. you should avoid using save and do partial updates or inserts depending on your need as you'll avoid problems such a two processes changing the same whole document and loosing changes.
>

Mooky

unread,
Nov 2, 2012, 6:29:43 AM11/2/12
to node-mongodb-native
And to re-ask the question, should it be possible to get a MongoError:
E11000 duplicate key error using save()?

Christian Kvalheim

unread,
Nov 2, 2012, 6:35:00 AM11/2/12
to node-mong...@googlegroups.com
Collection.prototype.save = function save(doc, options, callback) {
if('function' === typeof options) callback = options, options = null;
if(options == null) options = {};
if(!('function' === typeof callback)) callback = null;

var errorOptions = options.safe != null ? options.safe : false;
errorOptions = errorOptions == null && this.opts.safe != null ? this.opts.safe : errorOptions;
// Extract the id, if we have one we need to do a update command
var id = doc['_id'];

if(id) {
this.update({ _id: id }, doc, { upsert: true, safe: errorOptions }, callback);
} else {
this.insert(doc, { safe: errorOptions }, callback && function (err, docs) {
if (err) return callback(err, null);

if (Array.isArray(docs)) {
callback(err, docs[0]);
} else {
callback(err, docs);
}
});
}
};

the answer should be no if you look at the code which might mean there is another problem (like some other unique index somewhere)

Mooky

unread,
Nov 2, 2012, 6:42:08 AM11/2/12
to node-mongodb-native
Hmmm.

We dont have any other unique index.

We _are_ saving an array of docs with _id's. ie
collection.save([{_id : '50923b81e4b0b8b0e22bfbdd'},{_id :
'50919453e4b045be9d21563b'}])

Looking at the code, that might be the problem?

Christian Kvalheim

unread,
Nov 2, 2012, 6:47:49 AM11/2/12
to node-mong...@googlegroups.com
yeah that's not supported, it's only a single document save

Mooky

unread,
Nov 2, 2012, 6:56:12 AM11/2/12
to node-mongodb-native
Ok. I see.
Interestingly, it seems to be fine from the mongo console...

Mooky

unread,
Nov 2, 2012, 6:59:41 AM11/2/12
to node-mongodb-native
Actually, Im talking out my rear ...
Reply all
Reply to author
Forward
0 new messages