Mongoose - 如何使用来自父对象的信息自动更新子对象

Mongoose - how to update a child object with information from the parent automatically

有一个特定的原因需要通过以下方式获取信息,所以我无法更改父对象和子对象的关联方式:

我有一个父对象,其中包含有关多日交易会的信息。有一个子对象引用交易会每一天的信息,这些天中的每一天都被称为一个事件。我需要在生成公平对象时自动生成事件子对象。然后我需要从生成的子对象中单独生成一个 ObjectID 数组。此外,我需要能够在以后生成不基于公平对象的任意事件对象。这就是为什么我有 ObjectID 的数组,所以我知道什么是自动生成的,什么是用户生成的。

这是我的架构(只是有用的部分):

/**
* This is the main object, the user only supplies information for this object
* name, start and end are all provided by the user, num_days is automatically calculated
*/

var FairSchema = new Schema({
name: {
    type: String,
    default: '',
    required: 'Please fill Fair name',
    trim: true
},
start_date: {
    type: Date,
    required: 'Please provide a start date'
},
end_date: {
    type: Date,
    required: 'Please provide an end date'
},
num_days: {
    type: Number,
    required: true,
    default: 1
},
events: EventsSchema
});

/**
* This is the child object it should pull all of it's information from the parent at the time the parent is created if
* the parent object has more than one day an event child object is created for each day.
*
* name: The same as the parent object, unless multiday then it's the name plus 'Day #'
* start_date: The same as the parent object unless multiday then it's the parent object time + the child object day
* end_date: the same as the parent object unless multiday then it's the parent object time + the child object day
*
*/

var EventSchema = new Schema({
name: {
    type: String,
    required: 'Please provide a name for the event',
    trim: true
},
start_date: Date,
end_date: Date,
});

我在我的控制器中尝试过类似的东西,然后在保存公平对象之前调用它:

initializeFairEvents = function (fair) {
var fairLengthDays = fair.num_days;
var eventStart = Date.parse(fair.start_date);
var eventEnd = Date.parse(fair.end_date);
var one_day = (24 * 60 * 60 * 1000);
var events = fair.events;
var dailyEventLength = eventEnd - eventStart - one_day*(fairLengthDays-1);

if (events !== null && events.length > 0){}
else {
    for (var i = 0; i < fairLengthDays; i++) {
        var name, start_date, end_date, preserve;
        start_date = (i * one_day) + eventStart;
        end_date = (i * one_day) + eventStart + dailyEventLength;
        preserve = true;
        if (fairLengthDays === 1) {
            name = fair.name;
        } else {
            name = fair.name + ' - day ' + (i + 1).toString();
        }
        fair.events.push({
            name: name,
            start_date: start_date,
            end_date: end_date,
            preserve: preserve
        });
    }
}

};

但是我没有 ObjectID,因为没有任何东西进入数据库来生成 ID。

我觉得自己陷入了一个循环。任何提示或想法将不胜感激。

由于您使用的是 Mongoose,因此您可以从模型构造函数创建本地模型实例(假设 Event 为 Event),如下所示:

var event = Event();

或者如果你只有父模型实例(假设 fair 公平)你可以使用 subdoc create 方法:

var event = fair.events.create();

您还可以将对象包含到构造函数中,前提是它们与预期模型具有相同的形式。返回的实例将自动包含将由 MongoDB.

使用的对象 ID

就是说,subdoc 数组 push 方法应该自动执行此操作,并且每个数组项都应该有一个 ID。 None 这当然是在远程数据库中,直到您保存父文档。

var one_day = 86400000; // (24 * 60 * 60 * 1000) no sense in computing a static value

initializeFairEvents = function(fair) {
  // Assuming fair is a model instance
  var fairLengthDays = fair.num_days;
  var eventStart = Date.parse(fair.start_date);
  var eventEnd = Date.parse(fair.end_date);
  var events = fair.events;
  var dailyEventLength = eventEnd - eventStart - one_day * (fairLengthDays - 1);

  // It looks like you only want to add events if there are no events currently
  if (events === null || events.length === 0) {
    for (var i = 0; i < fairLengthDays; i++) {
      var newEvent = fair.events.create({
        name: (fairLengthDays === 1) ?
          fair.name :
          (fair.name + ' - day ' + (i + 1)), 
        start_date: (i * one_day) + eventStart,
        end_date: (i * one_day) + eventStart + dailyEventLength,
        preserve: true
      });

      // newEvent.id 

      fair.events.push(newEvent);
    }
  }
};