DalhousieDuck
DalhousieDuck

Reputation: 339

When using mongoimport, can you specify that the _id should be a UUID?

I am using mongoimport to import a large number of CSV records to MongoDb. Currently, by default, the _id field is being set to ObjectId, as shown:

"_id" : ObjectId("5fea8abcda05f098a1ac9857") 

Is there a parameter that I can pass to the mongoimport command that will cause MongoDb to generate a new UUID instead for each document as it is imported, such as:

"_id" : UUID("0ef6bc1d-017d-4c85-9393-2d7832598d03")

Otherwise, if it's not possible to assign a UUID to the _id field on import, is there another query I can write to perform this update on the imported collection?

Upvotes: 1

Views: 1757

Answers (2)

Biapy
Biapy

Reputation: 517

As far as I could find, mongoimport supports UUID with MongoDB Extended JSON (v2).

Extended json has Special rules for parsing $uuid fields.

According to documentation (and my tests), "_id" : UUID("0ef6bc1d-017d-4c85-9393-2d7832598d03") can be written as:

"_id" : { "$uuid": "0ef6bc1d-017d-4c85-9393-2d7832598d03" }

Unfortunately, this does not work with CSV (see Import UUID from CSV using “mongoimport”.

A ticket is opened to implement this functionality: TOOLS-2788.

My solution would be to use something like jq or yq, awk and bash to preprocess the csv files into json files with the UUID field written in the correct format, and then feed them to mongoimport`.

Upvotes: 1

x4k3p
x4k3p

Reputation: 1779

This seems to be not possible to switch to uuid when importing but you can do something like that:

mongoimport --db <your_db> --collection tmpCol --drop --type csv --file data.csv --headerline

And then:

db.tmpCol.find().forEach(function (document) {
  document._id = UUID()
  db.desiredCollection.insert(document)
})

Maybe relevant: https://forums.meteor.com/t/how-to-mongoimport-and-not-use-objectid-in-the--id-fields/23963/2

Upvotes: 3

Related Questions