I need to ensure that my Model has a unique "id" property, and I want this value to match the "_id" property of the document upon creation. Currently, my Model schema is set up like this:
const modelSchema = new Schema(
{
id: { type: String, unique: true },
property_1: { type: String, unique: false }
property_2: { type: String, unique: false }
...
}
);
At the moment, I achieve this by using two separate queries - one for creating the document, and another for updating the "id" property with the value of "_id" after the document is created.
Model.create({
propery_1: value_1,
propery_2: value_2,
...
}).then((document) => {
document.id = document._id;
document.save();
})
Is there a way to configure my Schema to automatically set the "id" property to the value of "_id"? Alternatively, is there a method to consolidate these two queries into one while maintaining the same functionality?