I\'m trying to dynamically create _id\'s for my Mongoose models by counting the documents in the db, and using that number to create the _id (assuming the first _id is 0). H
Create custom _id in mongoose and save that id as a mongo _id. Use mongo _id before saving documents like this.
const mongoose = require('mongoose');
const Post = new mongoose.Schema({
title: String,
content: String,
tags: [ String ]
}, { _id: false });
// request body to save
let post = new PostModel({
_id: new mongoose.Types.ObjectId().toHexString(), //5cd5308e695db945d3cc81a9
title: request.body.title,
content: request.body.content,
tags: request.body.tags
});
post.save();