Javascript 自增序列猫鼬

Javascript 自增序列猫鼬,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我在mongoose中实现了一个自动递增序列字段。我将默认值/起始值设置为5000。但它不是从5000开始,而是从1开始 这是我的密码: 我的计数器模式 // app/models/caseStudyCounter.js // load the things we need var mongoose = require('mongoose'); var bcrypt = require('bcrypt-nodejs'); // define the schema for our user

我在mongoose中实现了一个自动递增序列字段。我将默认值/起始值设置为5000。但它不是从5000开始,而是从1开始

这是我的密码:

我的计数器模式

// 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);
我的主模式:

// 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);
当我将默认值设置为5000时,我不明白为什么它的起始形式是1。顺序应该是5001、5002、5003,以此类推。非常感谢您的帮助。

您可以安装


它易于安装和使用。

可能这就是它发生的原因:

使用
setDefaultsOnInsert
选项。或者只需手动使用
{$inc:{n:1},$setOnInsert:{n:776}


我也面临同样的问题,所以我提出了这个解决方案

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 increament 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;

您可以安装此模块,您可以在之前创建该模块,以找到最后一个最大计数和增量保存。可能是由于插入错误,如果不存在文档,请尝试创建文档,然后更新它。可能相关/重复:有人说我mongodb是面向文档的数据库,不适合自动增量,那么,我可以制作一个没有自动递增编号的项目吗?当我们需要自动递增编号时,这完全取决于项目?是的,所以使用上面的。如果您的应用程序很重,请切换到另一个DB,如Redis
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 increament 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;