[DOCS-2191] undocumented behaviour or bug of db.collection.save (E11000 duplicate key error) Created: 05/Nov/13  Updated: 11/Jan/17  Resolved: 05/Nov/13

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: 01112017-cleanup

Type: Improvement Priority: Minor - P4
Reporter: Jaromir Muller [X] Assignee: Sam Kleinman (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 10 years, 15 weeks ago

 Description   

Hello,

recently I created a question in stackoverflow http://stackoverflow.com/questions/19787803/mongo-throws-e11000-duplicate-key-error-whey-i-try-to-do-db-collection-save-oper?noredirect=1#comment29414789_19787803

related to behaviour of function db.collection.save when I try to do insert/replace chunk of documents.

I created the scenario

> use sometestdb
switched to db sometestdb
> 
> doc1 = { _id: 1, value: "some value 1" }
{ "_id" : 1, "value" : "some value 1" }
> doc2 = { _id: 2, value: "some value 2" }
{ "_id" : 2, "value" : "some value 2" }
> doc3 = { _id: 3, value: "some value 3" }
{ "_id" : 3, "value" : "some value 3" }
> 
> db.docs.save( [doc1, doc2, doc3] )
> 
> doc1 = { _id: 1, value: "some value 1 - updated" }
{ "_id" : 1, "value" : "some value 1 - updated" }
> doc2 = { _id: 2, value: "some value 2 - updated" }
{ "_id" : 2, "value" : "some value 2 - updated" }
> doc3 = { _id: 3, value: "some value 3 - updated" }
{ "_id" : 3, "value" : "some value 3 - updated" }
> db.docs.save( [doc1, doc2, doc3] )
E11000 duplicate key error index: sometestdb.docs.$_id_  dup key: { : 1.0 }

The documentation does no say anything about using array of documents. Is it bug or ambiguous documentation?

Thank you, JM



 Comments   
Comment by Jaromir Muller [X] [ 06/Nov/13 ]

I agree with answer on SF, at least it makes sense. But I think it would fail when anything else then document is passed to db.collection.save. So db.collection.save ( [ doc1, doc2 ] ) should fail.

Comment by Sam Kleinman (Inactive) [ 05/Nov/13 ]

The question has been answered on stack overflow, and the answer is consistent with the documentation for the save method.

http://docs.mongodb.org/manual/reference/method/db.collection.save/#db.collection.save

Generated at Thu Feb 08 07:42:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.