Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

auto increment schema mongoose id

You can use this plugin: https://www.npmjs.com/package/mongoose-auto-increment

First you need to initialize it after creating Mongoose connection:

const connection = mongoose.createConnection("mongodb://localhost/myDatabase");
 
autoIncrement.initialize(connection);
Than in your subs.model.js file:

const mongoose = require('mongoose');
const autoIncrement = require('mongoose-auto-increment');

var subscriberSchema = new mongoose.Schema({
    _id: {type: String, required: true},
    email: {
        type: String
    }
}, {
    versionKey: false,
    collection: 'subscribers'
});

subscriberSchema.plugin(autoIncrement.plugin, {
    model: 'Subscribers',
    field: '_id'
});

module.exports = mongoose.model('Subscribers', subscriberSchema);
Comment

mongoose auto increment

// schema creation...

let modal;
SchemaVariable.pre('save', function(next) {
  if (this.isNew) {
    if (!modal) {
      modal = mongoose.model('collectionname');
    }
    modal.find({})
      .then((entries) => {
        this._id = entries.length + 1;
        next();
      })
  }
});
Comment

create auto increment mongodb mongoose

// using pre('save' on your mongoose schema

// creating the counter collection/model to save the number of documents:
const counterCollection = mongoose.model('counter',
	new mongoose.Schema({number: Number}), 'counter')

// updating the counter and the pre-saved document that need the index/number
documentSchema.pre('save', async function() {
  if(this.number) {
    let counterDoc = await counterCollection.findOne()
    if(!counterDoc) {
      counterDoc = new counterCollection({number: 1})
    } else {
      counterDoc.number++
    }
    this.number = counterDoc.number
    const response = await counterDoc.save()
  }
})
// don't forget to update the schema with the number key-value pair to
// store the number, so mongoose does not throw error
Comment

PREVIOUS NEXT
Code Example
Javascript :: array left rotation javascript 
Javascript :: string remove accents 
Javascript :: add jquery cdn 
Javascript :: today in moment 
Javascript :: copywithin javascript 
Javascript :: powershell json = get value by key 
Javascript :: eof while parsing 
Javascript :: javascript remove event listener 
Javascript :: how to create public folder in node js 
Javascript :: attr hidden to show jquery 
Javascript :: useLocation in jest 
Javascript :: js toggle boolean 
Javascript :: js image on canvas 
Javascript :: click outside box jquery 
Javascript :: reload table jquery 
Javascript :: get element with one or another class 
Javascript :: javascript last element of an array 
Javascript :: select remove option jquery 
Javascript :: bootstrap modal disable close on click outside react bootstrap 
Javascript :: patch swagger 
Javascript :: how to add checkbox in alert box in javascript 
Javascript :: como saber si una fecha es mayor que otra en javascript 
Javascript :: JavaScript how to put value 
Javascript :: register a service worker 
Javascript :: javascript confirm tab close 
Javascript :: mongoose update createdAt 
Javascript :: react native zindex issue on android 
Javascript :: console.time in javascript 
Javascript :: import json data in js file 
Javascript :: encodeuri hashtag 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =