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 :: change profile photo with javascript 
Javascript :: react component key prop 
Javascript :: transformar moeda real javascript 
Javascript :: how to write a funcat in javascript 
Javascript :: ejs public 
Javascript :: rivets bind 
Javascript :: Get the language of a user 
Javascript :: intersection of two objects in javascript 
Javascript :: js intellisence not working 
Javascript :: button remove class jquery 
Javascript :: async function javascript dec 
Javascript :: middleware uses 
Javascript :: export default module 
Javascript :: react axios Card List 
Javascript :: webpack dev srcipt 
Javascript :: api streamelements watchtime 
Javascript :: regular expression email 
Javascript :: how to create instance of class in javascript 
Javascript :: environment texture in three.js 
Javascript :: Play Audio Stream from Client 
Javascript :: find element in array underscore js 
Javascript :: javascript && operator 
Javascript :: react native stack transition from right to left 
Javascript :: comment faire pour écrire un texte en javascript 
Javascript :: how to change css using javascript 
Javascript :: javascript set elements width by tag name 
Javascript :: for each append to document 
Javascript :: JSON.stringify() function converts buffers into objects. The raw data is encoded as an array of bytes that you can pass in to Buffer.from(). 
Javascript :: react text docoration none 
Javascript :: javascript check type of variable var 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =