[英]Mongoose how to update array from child to parent
[英]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。 在保存父文檔之前,這些當然都不在遠程數據庫上。
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);
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.