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 :: node js simple server 
Javascript :: javascript subtract years from date 
Javascript :: add material angular 
Javascript :: push object into array javascript 
Javascript :: parse string javascript 
Javascript :: how to create a react app 
Javascript :: get search value from reacr route1 
Javascript :: classic asp json multidemsion json 
Javascript :: javascript img visible 
Javascript :: how to print hello world in javascript 
Javascript :: js check if object key exists 
Javascript :: Object.create Polyfill 
Javascript :: stop execution javascript 
Javascript :: node js dependency injection 
Javascript :: cogo toast 
Javascript :: How do I access a class without an instance? +javascript 
Javascript :: js get formatted time 
Javascript :: correct way to push into state array 
Javascript :: sidebar scroll css 
Javascript :: javascript Check Map Elements 
Javascript :: check if array does not contain string js 
Javascript :: jquery check if all elements hidden 
Javascript :: what is browse router 
Javascript :: reduce method in javascript 
Javascript :: webpack.config.js 
Javascript :: dayofweek mongodb 
Javascript :: jquery alertify 
Javascript :: smtp testing server 
Javascript :: javascript get last 2 item in array 
Javascript :: one signal api to send notification 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =