[DOCS-6035] Documentation for mongoimport and --upsertFields is ambigious around _id fields Created: 17/Aug/15  Updated: 30/Oct/23  Resolved: 01/Nov/22

Status: Closed
Project: Documentation
Component/s: manual, Server, tools
Affects Version/s: mongodb-2.6, mongodb-3.0
Fix Version/s: Server_Docs_20231030

Type: Task Priority: Major - P3
Reporter: Victor Hooi Assignee: Kay Kim (Inactive)
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Participants:
Days since reply: 1 year, 14 weeks, 1 day ago
Epic Link: DOCSP-1769

 Description   

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:

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.



 Comments   
Comment by Education Bot [ 01/Nov/22 ]

Hello! This ticket has been closed due to inactivity. If you believe this ticket is still important, please reopen it and leave a comment to explain why. Thank you!

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