-
Type: Task
-
Resolution: Won't Fix
-
Priority: Major - P3
-
Affects Version/s: mongodb-2.6, mongodb-3.0
-
Labels:None
If you attempt to do a mongoimport using --upsertFields, and your input file has a different _id field to what is in the destination database, mongoimport will not actually import that document - it will trigger an error message like this:
2015-08-17T12:37:00.321+1000 ns: cats.mycats 2015-08-17T12:37:00.321+1000 filesize: 81 2015-08-17T12:37:00.321+1000 got line:{ "_id" : { "$oid" : "55d148a5c4c71fce499a00b7" }, "name" : "ernie", "age" : 5 } 2015-08-17T12:37:00.326+1000 error: The _id field cannot be changed from {_id: ObjectId('55d148c2c4c71fce499a00b8')} to {_id: ObjectId('55d148a5c4c71fce499a00b7')}. 2015-08-17T12:37:00.327+1000 tried to import 1 objects
This is by design - mongod will not allow changing an _id field in an upsert operation.
However, both the 2.6 and 3.0 documentation do not make this sufficiently clear:
- https://docs.mongodb.org/v2.6/reference/program/mongoimport/#cmdoption--upsertFields
- https://docs.mongodb.org/v3.0/reference/program/mongoimport/#cmdoption--upsertFields
The 2.6 docs say:
Specifies a list of fields for the query portion of the upsert. Use this option if the _id fields in the existing documents don’t match the field in the document, but another field or field combination can uniquely identify documents as a basis for performing upsert operations.
which seems to imply you can use --upsertFields with another non-_id field, in order to change the _id field (you cannot).
The 3.0 docs say the same as the above, but also add:
Changed in version 3.0.0: Modifies the import process to update existing objects in the database if they match based on the specified fields, while inserting all other objects. You do not need to use --upsert with --upsertFields.
This excerpt basically just describes normal MongoDB upsert behaviour ("Modifies the import process to update existing objects in the database if they match based on the specified fields, while inserting all other objects"). It would be easier to simply state that --upsertFields implies --upsert.