简体   繁体   English

自动增加猫鼬模型中的数字字段

[英]Auto increment a number field in mongoose model

I was using MySql DB for my website and now updating it to MongoDB. 我在网站上使用MySql DB,现在将其更新到MongoDB。 There were columns in some tables getting auto-incremented and I want to make sure that the same continues. 有些表中的列会自动增加,我想确保同样的情况继续下去。 Below are my finding so far but none is working. 以下是我到目前为止的发现,但是没有任何结果。 Using mongoose-auto-increment plugin 使用mongoose-auto-increment插件

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    autoIncrement = require('mongoose-auto-increment');

var connection = mongoose.createConnection("mongodb://localhost/myDatabase");

autoIncrement.initialize(connection);

EmborderSchema.plugin(autoIncrement.plugin, {
  model: 'Emborder',
  field: 'emborder_id',
  startAt: 28855,
  increamentBy: 1
});

Using pre save hook: 使用预保存钩子:

EmborderSchema.pre('save', function (next) {
  if (this.emborder_id == undefined) {
    Emborder.findOne({})
      .sort({emborder_id: -1})
      .exec(function (err, data) {
        this.emborder_id = data.emborder_id + 1;
        next();
      })
  } else {
    next();
  }
});

Using the first method gives me an error 使用第一种方法给我一个错误

E11000 duplicate key error collection: 24hd.24hd_emborder index: emborder_id_1 dup key: { 23345 } E11000重复密钥错误集合:24hd.24hd_emborder索引:emborder_id_1 dup密钥:{23345}

Second method gives this error 第二种方法给出此错误

E11000 duplicate key error collection: 24hd.24hd_emborder index: emborder_id_1 dup key: { : null } E11000重复密钥错误集合:24hd.24hd_emborder索引:emborder_id_1 dup密钥:{:null}

Note: emborder_id was the primary key in my old Mysql DB and in this code value of emborder_id is undefined as I am adding a new record. 注意:emborder_id是我的旧MySQL数据库中的主键,在我添加新记录时,emborder_id的此代码值未定义。

Please help me finding whats going wrong in this approch. 请帮助我查找此方法中出了什么问题。

I will recommend second approach for you. 我将为您推荐第二种方法。

EmborderSchema.pre('save', function (next) {
  var self = this;
  if (self.emborder_id == undefined) {
    Emborder.findOne({})
      .sort({emborder_id: -1})
      .exec(function (err, data) {
        if(!data.emborder_id){
         self.emborder_id = 1;
         next();
        }else{
          self.emborder_id = data.emborder_id + 1;
          next();
        }
      })
    next();
  }
});

For more you can read about Context in javascript. 有关更多信息,请阅读javascript中的Context

For example , inside of a function, when you say: “this.accoutNumber”, you are referring to the property “accoutNumber”, that belongs to the object within which the function is executing. 例如 ,在函数内部,当您说:“ this.accoutNumber”时,您所指的是属性“ accoutNumber”,该属性属于在其中执行该函数的对象。

If the object “foo” has a method called “bar”, when the JavaScript keyword “this” is used inside of “bar”, it refers to “foo”. 如果对象“ foo”具有称为“ bar”的方法,则在“ bar”内部使用JavaScript关键字“ this”时,它表示“ foo”。 If the function “bar” were executed in the global scope, then “this” refers to the window object. 如果函数“ bar”是在全局范围内执行的,则“ this”是指窗口对象。

Context example taken from --> https://blog.kevinchisholm.com/javascript/difference-between-scope-and-context/ 上下文示例摘自-> https://blog.kevinchisholm.com/javascript/difference-between-scope-and-context/

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

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