Model.js const Mongoose = require('mongoose');const Schema = Mongoose.Schema;let counter = 1;let CountedId = {type: Number, default: () = counter++};const ModelSchema = new Schema({ id: CountedId, // ....});const Model = Mongoose.model('Mod
const Mongoose = require('mongoose'); const Schema = Mongoose.Schema; let counter = 1; let CountedId = {type: Number, default: () => counter++}; const ModelSchema = new Schema({ id: CountedId, // .... }); const Model = Mongoose.model('Model', ModelSchema); module.exports = Model; Model.find({ id: { $gt: 0 } }).sort({ id: -1 }) .then(([first, ...others]) => { if (first) counter = first.id + 1; });