mongodbmongoimport

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?


Solution

  • 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