简体   繁体   English

自动递增序列 Mongoose

[英]Auto Increment Sequence Mongoose

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.我将默认/起始值设置为 5000。但它不是从 5000 开始,而是从 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.当我将默认值设置为 5000 时,我无法弄清楚为什么它的起始形式 1。序列应该是 5001、5002、5003 等等。 Any help will be greatly appreciated.任何帮助将不胜感激。

Probably this is why it happens: https://github.com/Automattic/mongoose/issues/3617#issuecomment-160296684可能这就是它发生的原因: https : //github.com/Automattic/mongoose/issues/3617#issuecomment-160296684

Use the setDefaultsOnInsert option.使用setDefaultsOnInsert选项。 Or just manually use {$inc: {n:1}, $setOnInsert: {n:776} }或者只是手动使用{$inc: {n:1}, $setOnInsert: {n:776} }

You can install mongoose-auto-increment .您可以安装mongoose-auto-increment

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

It's easy to install and use.它易于安装和使用。

This is such a common need that it might be worthwhile using an existing module. 这是一个常见的需求,使用现有模块可能是值得的。 I played around with a few, but this was the easiest to get to work with my project. 我玩了几个,但这是最容易使用我的项目。 https://www.npmjs.com/package/mongoose-sequence . https://www.npmjs.com/package/mongoose-sequence Remember if you want to repurpose the _id field, you must declare it as a Number type in your schema as per the docs. 请记住,如果要重新调整_id字段的用途,则必须根据文档将其声明为模式中的Number类型。

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

You can use this module. 您可以使用此模块。

mongoose-easy-auto-increment 猫鼬,易于自动递增

Wish it can help you. 希望它可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM