'how can I set custom _id for a document in mongo-DB

I want to upload a data to mongodb and set a custom id for content in a file which is a sub-document in the collection--

{
   _id: document id...,
   FirstName: ...,
   LasrName: ...,
   Email: ...,
   files: [
   content: [
    _id: objectId(...)

] ],

}



Solution 1:[1]

you can set a custom Id for the creation of data by using this schema here. nanoid is rundom string.

const mySchema = new Schema({
  _id: {
    type: String,
    default: () => nanoid()  /// random_string
  }
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RONAK SUTARIYA