Answers for "auto increment in mongoose + nodejs"

-2

auto increment in mongoose + nodejs

...
var bookSchema = new Schema({
    author: { type: Schema.Types.ObjectId, ref: 'Author' },
    title: String,
    genre: String,
    publishDate: Date
});

bookSchema.plugin(autoIncrement.plugin, 'Book');
var Book = connection.model('Book', bookSchema);
````

That's it. Now you can create book entities at will and they will have an `_id` field added of type `Number` and will automatically
 increment with each new document. Even declaring references is easy, just remember to change the reference property's type
 to `Number` instead of `ObjectId` if the referenced model is also using the plugin.

````js
var authorSchema = new mongoose.Schema({
...
Posted by: Guest on January-25-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language