自增序列Mongoose

4
我在mongoose中实现了一个自增序列字段。我将默认/起始值设置为5000。但它没有从5000开始,而是从1开始。
以下是我的代码:
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);

我的主要架构:

// 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);

我无法理解为什么它从1开始,当我已经将默认值设为5000。 序列应该是5001,5002,5003等等。 如有帮助,将不胜感激。

你可以安装这个模块mongoose-auto-increment - morels
你可以先创建一个变量来获取最后一个最大计数,然后将其递增并保存。 - Chandra Kant Paliwal
可能是由于upsert的原因,您能否尝试创建文档(如果不存在)然后再更新它? - Naramsim
可能相关/重复:https://dev59.com/AYXca4cB1Zd3GeqPDBSn - Naramsim
3个回答

2

有人告诉我,MongoDB是面向文档的数据库,不适合自动增量,那么我可以做一个没有自动增量数字的项目吗? - Saeed Heidarizarei
这完全取决于项目。 - Naramsim
我们什么时候需要自动增量数字? - Saeed Heidarizarei
好的,那就使用上述方法。如果您的应用程序较重,请切换到其他数据库,例如Redis。 - Naramsim

0

你可以安装mongoose-auto-increment

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

安装和使用都非常简单。


0

我之前也遇到了同样的问题,所以我想出了这个解决方案。

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接