0
我正在努力與貓鼬的人口。假設我有一個包含許多嵌套對象的大文檔,這些嵌套對象並不總是得到Schema,或者僅僅是Type.Mixed。如何在貓鼬中異步填充嵌套對象?
架構這個文件將是這樣的:
{
token: String,
group: String,
connectionStatus: String,
activeQuestLine: String,
quests:
{
exposition: [
{
content: { type: mongoose.Schema.Types.ObjectId, ref: 'Quest' },
status: String,
trigger:
{
name: String,
param: mongoose.Schema.Types.Mixed
},
subQuests:
[
{
content: { type: mongoose.Schema.Types.ObjectId, ref: 'Quest' },
status: String,
trigger:
{
name: String,
param: mongoose.Schema.Types.Mixed
}
}
]
}
],
conflict: [
{
content: { type: mongoose.Schema.Types.ObjectId, ref: 'Quest' },
status: String
}
],
resolution: [
{
content: { type: mongoose.Schema.Types.ObjectId, ref: 'Quest' },
status: String
}
]
},
inventory:
{
maxSlots: Number,
slots: [
{
itemType: String,
content: mongoose.Schema.Types.Mixed,
empty: Boolean
}
]
}
};
所以我會'findOne'這種類型的文件一次,在需要的時候填充某些部分對我的具體使用情況進行保存,然後。據我所見,這個主題的大部分問題都是指在操作後保存填充文檔,但我想保持原樣,並將(填充的)數據傳遞給其他某個函數。
例如,我只想獲得某個博覽會物業的所有「subQuests」。在那些也可以有他們的'subQuests'的地方,是的,應該有一個遞歸的Schema,但是現在這不是主要問題。問題是我想像這樣遍歷它:
async.forEachSeries(
questInfo.subQuests,
function (subQuestInfo, eachCallback)
{
QuestManager.getQuest(subQuestInfo.content, function (subQuest)
{
if (!subQuest) throw 'SubQuest ' + subQuestInfo.content + ' not found';
subQuestInfo.content = subQuest;
eachCallback();
});
},
function (err)
{
if (err) throw err;
activeQuests.push(questInfo);
});
questInfo從主文檔中提取並作爲參數傳遞給該功能。但是在這一行:
subQuestInfo.content = subQuest;
'content'的值仍然只是ObjectId,所以是以前的值。我的任務顯然沒有影響!這是一種人工嘗試,有沒有更好的方法來解決這個問題?
謝謝你的時間。
謝謝您的回答。我其實沒有quest.subQuests,而是GameClient.quests.exposition [0] .subQuests。這可以確保我具有描述性的任務數據和任務安排以及它們之間的關係。 – mrpool89
不客氣!遊戲客戶端是貓鼬模式嗎? – lipp
是的。這基本上是我上面發佈的那個。 – mrpool89