I have been populating MongoDB collections with JSON data via MongoDB Compass using two methods:
Through the mongosh shell with the command:
mongoimport --jsonArray --authenticationDatabase admin --username <username> --password <password> -c <collection name> -d <database name> drop --file <filename>
and by importing the data through the Compass interface GUI when in the collection view.
Is there a way to set the primary key _id
to generate a UUID instead of the ObjectID when populating the collection with a JSON file?
Using UUIDs instead of ObjectIDs in MongoDB is a discussion of the possibilities, pros, and cons of using UUIDs instead of ObjectIDs, but it doesn't discuss how to set the primary key to be a UUID instead of ObjectID, particularly when populating the database with JSON data.