所以,我有這個專案模型,它有一個所有者,可以是用戶或團隊
const projectSchema = new Schema<IProject>({
projectName: {
type: String,
required: true
},
description: {
type: String,
required: false
},
site: {
type: String,
required: false
},
owner: {
type: Schema.Types.ObjectId,
required: true,
refPath: 'ownerModel'
},
ownerModel: {
type: String,
required: true,
enum: ['Team', 'User']
},
slug: {
type: String,
required: true,
unique: true,
dropDups: true
},
keywords: {
type: [String],
required: true
}
})
我遇到的問題是當所有者是一個團隊時,因為團隊可以有多個所有者,就像這樣
const teamSchema = new Schema<ITeam>({
teamName: {
type: String,
required: true
},
owner: {
type: [Schema.Types.ObjectId],
required: true,
ref: 'User'
},
slug: {
type: String,
required: true,
unique: true
},
bio: {
type: String,
required: false
},
verified: {
type: Boolean,
required: true,
default: false
},
quote: {
type: String,
required:false
},
members: [{
member: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
status: {
type: String,
required: true,
enum: ["accepted", "pending"]
},
type: {
type: String,
required: true,
enum: ["admin", "coworker"]
}
}],
followers: {
type: Number,
required: true
},
logo: {
type: String,
required: false
},
type: {
type: String,
required: true
},
})
雖然我必須填充專案的所有者欄位(也就是加載團隊),但我不能終生讓團隊的所有者也不能通過填充加載它的成員。
我嘗試了 owner.owner,嘗試將模型更改為不使用陣列,嘗試使用 deepPopulate 插件但出現型別錯誤。沒有任何效果
uj5u.com熱心網友回復:
我發現了怎么做,我所要做的就是正確使用貓鼬物件,不知道我是怎么讓它過去的。這是它
let projetos = await ProjectModel.find({owner: args.id, ownerModel: "Team"})
.populate({ path: 'owner',
populate: [
{
path: 'owner'
},
{
path: 'members'
}
]
});
我使用了一個陣列,因為我想填充多個欄位。關于這個的貓鼬檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/533118.html
標籤:打字稿mongodb猫鼬
下一篇:如何將物體欄位添加到加入的檔案?
