Mongoimport using CSV and columnsHaveTypes - how to specify an ObjectID?


I am trying to use mongoimport --mode-merge to apply some bulk updates to a collection. I want to update existing documents that all have _id : ObjectIDs formatted ids.

eg: mongoimport -d=database -c=collection --mode=merge --file=patch.csv -type=csv - columnsHaveTypes

As the title suggests I am trying to import CSV data into a collection using mongoimport and a formatted CSV file. I know using the columnsHaveTypes switch I can include data types in my header line.

But in the documentation there is no mention of how to specify an ObjectID or format it in the CSV.

Any suggestions?

ps. I have been able to achieve the correct result by switching to JSON but that now involves an extra step hence the interest columnsHaveTypes.

Hi :wave: @Daniel_Alvers,

Welcome to the MongoDB Community forums :sparkles:

As per mongoimport documentation, the type of columnsHaveTypes can be one of:

auto, binary, boolean, date, date_go, date_ms, 
date_oracle, decimal, double, int32, int64, string

If you want to do the import manually though, you can write a script or convert the CSV file into EJSON before using the mongoimport.

Alternatively, MongoDB Compass, however, lets you upload a CSV file, and then choose ObjectID from the drop-down menu when you upload your CSV file.

Furthermore, I suggest you post this feature on

I hope it helps!



Brilliant response Kushagra!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.