最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Auto Increment Sequence Mongoose - Stack Overflow

programmeradmin0浏览0评论

I implemented a auto increment sequence field in mongoose. I set the default/starting value as 5000. But it does not start from 5000, it starts from 1.

Heres my code:

My Counter Schema

// app/models/caseStudyCounter.js
// load the things we need
var mongoose = require('mongoose');
var bcrypt   = require('bcrypt-nodejs');

// define the schema for our user model
var caseStudyCounterSchema = mongoose.Schema({
   _id: {type: String, required: true},
   seq: {type: Number, default: 5000}

});

// methods ======================

// create the model for users and expose it to our app
module.exports = mongoose.model('caseStudyCounter', caseStudyCounterSchema);

My Main Schema:

// grab the mongoose module
var caseStudyCounter = require('../models/caseStudyCounter');
var mongoose = require("mongoose");

// grab the bcrypt module to hash the user passwords
var bcrypt   = require('bcrypt-nodejs');

// define the schema for our model
var caseStudySchema = mongoose.Schema({
     caseStudyNo: Number,
     firstName: String,
     lastName: String,

 });

caseStudySchema.pre('save', function(next) {
  var doc = this;

caseStudyCounter.findByIdAndUpdate({_id: 'caId'},{$inc: { seq: 1}},{"upsert": true,"new": true  }, function(error, counter)   {
    if(error)
        return next(error);
    doc.caseStudyNo = counter.seq;
    next();
});

});
 // module.exports allows us to pass this to other files when it is called
 // create the model for users and expose it to our app
 module.exports = mongoose.model('CaseStudy', caseStudySchema);

I can't figure out why its starting form 1 when I have set the default as 5000. The sequence should be 5001, 5002, 5003 and so on. Any help will be greatly appreciated.

I implemented a auto increment sequence field in mongoose. I set the default/starting value as 5000. But it does not start from 5000, it starts from 1.

Heres my code:

My Counter Schema

// app/models/caseStudyCounter.js
// load the things we need
var mongoose = require('mongoose');
var bcrypt   = require('bcrypt-nodejs');

// define the schema for our user model
var caseStudyCounterSchema = mongoose.Schema({
   _id: {type: String, required: true},
   seq: {type: Number, default: 5000}

});

// methods ======================

// create the model for users and expose it to our app
module.exports = mongoose.model('caseStudyCounter', caseStudyCounterSchema);

My Main Schema:

// grab the mongoose module
var caseStudyCounter = require('../models/caseStudyCounter');
var mongoose = require("mongoose");

// grab the bcrypt module to hash the user passwords
var bcrypt   = require('bcrypt-nodejs');

// define the schema for our model
var caseStudySchema = mongoose.Schema({
     caseStudyNo: Number,
     firstName: String,
     lastName: String,

 });

caseStudySchema.pre('save', function(next) {
  var doc = this;

caseStudyCounter.findByIdAndUpdate({_id: 'caId'},{$inc: { seq: 1}},{"upsert": true,"new": true  }, function(error, counter)   {
    if(error)
        return next(error);
    doc.caseStudyNo = counter.seq;
    next();
});

});
 // module.exports allows us to pass this to other files when it is called
 // create the model for users and expose it to our app
 module.exports = mongoose.model('CaseStudy', caseStudySchema);

I can't figure out why its starting form 1 when I have set the default as 5000. The sequence should be 5001, 5002, 5003 and so on. Any help will be greatly appreciated.

Share Improve this question asked Jun 15, 2016 at 9:52 SkywalkerSkywalker 5,19417 gold badges65 silver badges127 bronze badges 4
  • you can install this module mongoose-auto-increment – morels Commented Jun 15, 2016 at 10:00
  • you can create before to get find one last largest count and incremented save. – Chandra Kant Paliwal Commented Feb 21, 2018 at 14:35
  • Probably is because of the upsert, can you please try creating the document if not exists and than update it? – Naramsim Commented Feb 22, 2018 at 10:46
  • Possible related/duplicate: stackoverflow./questions/26865357/… – Naramsim Commented Feb 22, 2018 at 10:47
Add a ment  | 

3 Answers 3

Reset to default 2 +50

Probably this is why it happens: https://github./Automattic/mongoose/issues/3617#issuement-160296684

Use the setDefaultsOnInsert option. Or just manually use {$inc: {n:1}, $setOnInsert: {n:776} }

You can install mongoose-auto-increment.

yourSchema.plugin(autoIncrement.plugin, {
    model: 'model',
    field: 'field',
    startAt: 5000,
    incrementBy: 1
});

It's easy to install and use.

I was also facing same issue so i came up with this solution.

var mongoose = require("mongoose");

// define the schema for our model
var caseStudySchema = mongoose.Schema({
 caseStudyNo: {
  type:Number,
  default:5000
 },
 firstName: String,
 lastName: String,
});

caseStudySchema.pre('save', function(next) {

var doc = this;

//Retrieve last value of caseStudyNo
CaseStudy.findOne({},{},{sort: { 'caseStudyNo' :-1}}, function(error, counter)   {
//if documents are present in collection then it will increment caseStudyNo 
// else it will create a new documents with default values 
 
    if(counter){
      counter.caseStudyNo++;
      doc.caseStudyNo=counter.caseStudyNo;
    }
    next();
 });
});
// module.exports allows us to pass this to other files when it is called
// create the model for users and expose it to our app
const CaseStudy = mongoose.model('CaseStudy', caseStudySchema);
module.exports = CaseStudy;
发布评论

评论列表(0)

  1. 暂无评论