The task is to change default unique _id in mongoDb to autoincremented id with that kind of view 00001, 00002, I have done only autoincrement like that 1, 2 , but not sure is that way right or no
here is my autoincrement code:
//here we sort by id and found the last added product
const lastProd = await this.productModel
.findOne()
.sort({ _id: 'desc' })
.exec();
const newProduct = new this.productModel({
//if last prod is not null, do increment
_id: lastProd ? lastProd._id + 1 : 0,
title,
description: desc,
price,
image:image.filename,
});
const result = await newProduct.save();
photo of result of my code
