Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 定制猫鼬id:两种方法,优缺点是什么?_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js 定制猫鼬id:两种方法,优缺点是什么?

Node.js 定制猫鼬id:两种方法,优缺点是什么?,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,简单用例: 让我们从这个简单的模型开始: // Schema var notebookSchema = new mongoose.Schema({ title: { type: 'string', required: true } }); 使用此模型,Mongo中的对象的形式如下: { _id: 558ab637cab9a2b01dae9a97, title:"a notebook title" } 我不想要这种ID。我知道有一些像或那样的快速中间件可以做到这一点

简单用例:

让我们从这个简单的模型开始:

// Schema
var notebookSchema = new mongoose.Schema({
  title: {
    type: 'string',
    required: true
  }
});
使用此模型,Mongo中的对象的形式如下:

{ _id: 558ab637cab9a2b01dae9a97,
  title:"a notebook title"
}
我不想要这种ID。我知道有一些像或那样的快速中间件可以做到这一点,但我不想使用外部依赖项

因此,我认为有两种方法可以做到这一点:

1。使用与我上面提到的libs类似的代码,如下所示:

var mongoose = require('mongoose'),
  Schema = mongoose.Schema,
  Types = mongoose.Types,
  mongooseSave = mongoose.Model.prototype.save,
  SchemaString = mongoose.Schema.Types.String;

function CustomId (key) {
  SchemaString.call(this, key);
};

CustomId.prototype.__proto__ = SchemaString.prototype;
Schema.Types.CustomId = CustomId;
Types.CustomId = String;

mongoose.Model.prototype.save = function(callback) {
  for (field in this.schema.tree) {
    if (this.isNew && this[field] === undefined) {
      var fieldType = this.schema.tree[field];
      if (fieldType === CustomId || fieldType.type === CustomId) {
        var oid = mongoose.Types.ObjectId();
        var id = new Buffer('' + oid, 'hex').toString('base64').replace('+', '-').replace('/', '_');
        this[field] = id;
        mongooseSave.call(this, function(err, obj) {
          callback(err, obj);
        });
        return;
      }
    }
  }
  mongooseSave.call(this, callback);
};
module.exports = exports = CustomId;
// Schema
var notebookSchema = new mongoose.Schema({
  _id: {
    type: 'string'
  },
  title: {
    type: 'string',
    required: true
  }
});

var customIdPlugin = function(schema, options) {
  schema.pre('save', function(next) {
    var oid = ""+mongoose.Types.ObjectId();
    this._id = new Buffer(oid, 'hex').toString('base64').replace('+', '-').replace('/', '_');;
    next();
  });
};

notebookSchema.plugin(customIdPlugin );
用法:

var mongoose = require("mongoose");
var CustomId = require('../utils/custom_id');
// Schema
var BookSchema = new mongoose.Schema({
  _id: {
    type: CustomId
  },
  title: {
    type: "string",
    required: true
  }
});
2。使用插件方法,如下所示:

var mongoose = require('mongoose'),
  Schema = mongoose.Schema,
  Types = mongoose.Types,
  mongooseSave = mongoose.Model.prototype.save,
  SchemaString = mongoose.Schema.Types.String;

function CustomId (key) {
  SchemaString.call(this, key);
};

CustomId.prototype.__proto__ = SchemaString.prototype;
Schema.Types.CustomId = CustomId;
Types.CustomId = String;

mongoose.Model.prototype.save = function(callback) {
  for (field in this.schema.tree) {
    if (this.isNew && this[field] === undefined) {
      var fieldType = this.schema.tree[field];
      if (fieldType === CustomId || fieldType.type === CustomId) {
        var oid = mongoose.Types.ObjectId();
        var id = new Buffer('' + oid, 'hex').toString('base64').replace('+', '-').replace('/', '_');
        this[field] = id;
        mongooseSave.call(this, function(err, obj) {
          callback(err, obj);
        });
        return;
      }
    }
  }
  mongooseSave.call(this, callback);
};
module.exports = exports = CustomId;
// Schema
var notebookSchema = new mongoose.Schema({
  _id: {
    type: 'string'
  },
  title: {
    type: 'string',
    required: true
  }
});

var customIdPlugin = function(schema, options) {
  schema.pre('save', function(next) {
    var oid = ""+mongoose.Types.ObjectId();
    this._id = new Buffer(oid, 'hex').toString('base64').replace('+', '-').replace('/', '_');;
    next();
  });
};

notebookSchema.plugin(customIdPlugin );

这两种方法都很好,我知道没有“官方魔法标准化”的方法,但是你能告诉我你会选择什么以及为什么(赞成和反对)

如果我必须选择,我会选择插件版本,因为它不需要monkeypatching。不过,为什么不想使用ObjectId呢?我不想使用ObjectId,因为它们的长度(24)。在上面的示例中,我使用了B64 ObjectId,将其长度减少到16,但也可以是另一个生成器,就像短id使用的生成器一样(自定义长度、字母表等)。
ObjectId
的使用范围。当然,但是对象的十六进制字符串表示形式中有24个字符……如果我必须选择,我会选择插件版本,因为它不需要修补。不过,为什么不想使用ObjectId呢?我不想使用ObjectId,因为它们的长度(24)。在上面的示例中,我使用了B64 ObjectId,将其长度减少到16,但也可以是另一个生成器,就像短id使用的生成器(自定义长度、字母表等)。
ObjectId
的使用。当然,但对象的十六进制字符串表示形式中有24个字符。。。