Mongoose自定义模式类型日期间隔

时间:2022-05-07 16:28:32

I have many fields in my documents of type date intervals, such as this

在我的文档中,我有许多类型为日期间隔的字段,比如这个

{
    publishDate:
    {
       start: {type: Date, required: true},
       end: {type: Date, required: true}
    }
}

To reduce duplication of the code and make it easier to maintain, how to create custom Mongoose type, for instance DateInterval, containing two fields:

为了减少代码的重复并使其易于维护,如何创建自定义的Mongoose类型,例如DateInterval,包含两个字段:

  1. start
  2. 开始
  3. end
  4. 结束

and containing validator that makes sure both fields are filled out, and start is before end?

并且包含确保两个字段都被填满,并且start在end之前的验证器?

3 个解决方案

#1


3  

You can reuse schemas in mongoose.

可以在mongoose中重用模式。

var DateIntervalSchema = new Schema({
   start: {type: Date, required: true},
   end: {type: Date, required: true}
});

var SomeSchema = new Schema({
   publishDate: [DateIntervalSchema],
   // ... etc
});

You can also reference documents from other collections.

您还可以引用来自其他集合的文档。

var SomeSchema = new Schema({
   publishDate: {type: Schema.ObjectId, ref: 'DateInterval'}
});    

//using populate
SomeModel.findOne({ someField: "value" })
   .populate('publishDate') // <--
   .exec(function (err, doc) {
      if (err) ...

   })

#2


1  

You'll want to develop a custom schema type. There are a number of plugins that do this already, one of which, for long numbers, can be found here: https://github.com/aheckmann/mongoose-long/blob/master/lib/index.js . This is a good basic example to follow.

您将希望开发自定义模式类型。现在已经有很多这样做的插件了,其中有一个,对于长数据,可以在这里找到:https://github.com/aheckmann/mongoose-long/blob/master/lib/index.js。这是一个很好的基本例子。

For your purposes, then, you can create a DateInterval custom schema, casting it as type Date, and then use a validator to check start and end - http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate.

出于您的目的,您可以创建一个DateInterval定制模式,将其转换为类型日期,然后使用验证器来检查启动和结束- http://mongoosejs.com/docs/api.html# schematype_schematype_validate。

#3


0  

Since >=4.4 you can implement your custom schema type.

由于mongoose >=4.4,您可以实现自定义模式类型。

Documentation is not very clear, but you can follow this example.

文档不是很清楚,但是您可以遵循这个示例。

You have to:

你必须:

  • define your DateInterval custom object with toBSON() / toJSON() and toObject() prototype methods

    使用toBSON() / toJSON()和toObject()原型方法定义您的DateInterval自定义对象

  • define the DateIntervalType inherited from mongoose.SchemaType for handle the mongoose integration, and casting to DateInterval.

    定义从mongoose继承的DateIntervalType。用于处理mongoose集成并将其转换为DateInterval的模式类型。

In this way you can achieve full control on memory (Mongoose model) and mongodb (raw's bson) data representation.

通过这种方式,您可以实现对内存(Mongoose模型)和mongodb (raw's bson)数据表示的完全控制。

#1


3  

You can reuse schemas in mongoose.

可以在mongoose中重用模式。

var DateIntervalSchema = new Schema({
   start: {type: Date, required: true},
   end: {type: Date, required: true}
});

var SomeSchema = new Schema({
   publishDate: [DateIntervalSchema],
   // ... etc
});

You can also reference documents from other collections.

您还可以引用来自其他集合的文档。

var SomeSchema = new Schema({
   publishDate: {type: Schema.ObjectId, ref: 'DateInterval'}
});    

//using populate
SomeModel.findOne({ someField: "value" })
   .populate('publishDate') // <--
   .exec(function (err, doc) {
      if (err) ...

   })

#2


1  

You'll want to develop a custom schema type. There are a number of plugins that do this already, one of which, for long numbers, can be found here: https://github.com/aheckmann/mongoose-long/blob/master/lib/index.js . This is a good basic example to follow.

您将希望开发自定义模式类型。现在已经有很多这样做的插件了,其中有一个,对于长数据,可以在这里找到:https://github.com/aheckmann/mongoose-long/blob/master/lib/index.js。这是一个很好的基本例子。

For your purposes, then, you can create a DateInterval custom schema, casting it as type Date, and then use a validator to check start and end - http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate.

出于您的目的,您可以创建一个DateInterval定制模式,将其转换为类型日期,然后使用验证器来检查启动和结束- http://mongoosejs.com/docs/api.html# schematype_schematype_validate。

#3


0  

Since >=4.4 you can implement your custom schema type.

由于mongoose >=4.4,您可以实现自定义模式类型。

Documentation is not very clear, but you can follow this example.

文档不是很清楚,但是您可以遵循这个示例。

You have to:

你必须:

  • define your DateInterval custom object with toBSON() / toJSON() and toObject() prototype methods

    使用toBSON() / toJSON()和toObject()原型方法定义您的DateInterval自定义对象

  • define the DateIntervalType inherited from mongoose.SchemaType for handle the mongoose integration, and casting to DateInterval.

    定义从mongoose继承的DateIntervalType。用于处理mongoose集成并将其转换为DateInterval的模式类型。

In this way you can achieve full control on memory (Mongoose model) and mongodb (raw's bson) data representation.

通过这种方式,您可以实现对内存(Mongoose模型)和mongodb (raw's bson)数据表示的完全控制。