Mongoose嵌套数组populate不起作用

huangapple go评论65阅读模式
英文:

Mongoose populate nested array not working

问题

我遇到了一个问题,无法填充Mongoose中的嵌套文档数组,而我在其他帖子中看到的解决方案似乎都不起作用。

以下是与我的问题相关的模型:

比赛(Match)

const MatchSchema = new Schema({
    home: {
        type: Schema.Types.ObjectId,
        ref: 'Team',
    },
    away: {
        type: Schema.Types.ObjectId,
        ref: 'Team',
    },
    competition: {
        type: Schema.Types.ObjectId,
        ref: 'Competition',
    },
    date: Date,
    live: Boolean,
    odds: [
        {
            type: Schema.Types.ObjectId,
            ref: 'Odd',
        }
    ],
})

赔率(Odd)

const OddSchema = new Schema({
    oddValue: Number,
    option: {
        type: Schema.Types.ObjectId,
        ref: 'Option'
    },
    website: {
        type: Schema.Types.ObjectId,
        ref: 'Website'
    },
    market: {
        type: Schema.Types.ObjectId,
        ref: 'Market'
    },
    match: {
        type: Schema.Types.ObjectId,
        ref: 'Match'
    },
})

市场(Market)

const MarketSchema = new Schema({
    market: String,
    period: String,

    options: [
        {
            option: {
                type: Schema.Types.ObjectId,
                ref: 'Option'
            },
        }
    ],
})

选项(Option)

const OptionSchema = new Schema({
    name: String,
})

以下是我目前如何填充文档的方式:

let matches = await Match.find()
    .populate('home')
    .populate('away')
    .populate({
        path: 'competition',
        populate: [
            { path: 'country', model: 'Country' },
            { path: 'sport', model: 'Sport'}
        ]
    })
    .populate({
        path: 'odds',
        populate: [
            { path: 'option', model: 'Option' },
            { path: 'website', model: 'Website' },
            { path: 'market', model: 'Market' }
        ]
    })

我在填充嵌套的市场文档中的'option'字段时遇到了问题。

我尝试了链式调用populate,如下所示:

.populate({
    path: 'odds',
    populate: {
        { path: 'market', model: 'Market', 
          populate: { path:'options.option', model: 'Option' }}
    }
})

但我一直得到相同的奇数文档输出,其中所有字段都成功填充,除了嵌套市场字段中的options数组。

奇数输出

我组织数据的方式是否有问题?我是否错误地调用了populate?感谢您提前的帮助。

英文:

I'm having an issue populating a nested array of documents with Mongoose and none of the solutions I've seen in other threads seem to work.

Here are the models relevant to my issue:

Match

const MatchSchema = new Schema({
    home: {
        type: Schema.Types.ObjectId,
        ref: 'Team',
    },
    away: {
        type: Schema.Types.ObjectId,
        ref: 'Team',
    },
    competition: {
        type: Schema.Types.ObjectId,
        ref: 'Competition',
    },
    date: Date,
    live: Boolean,
    odds: [
        {
            type: Schema.Types.ObjectId,
            ref: 'Odd',
        }
    ],
})

Odd

const OddSchema = new Schema({
    oddValue: Number,
    option: {
        type: Schema.Types.ObjectId,
        ref: 'Option'
    },
    website: {
        type: Schema.Types.ObjectId,
        ref: 'Website'
    },
    market: {
        type: Schema.Types.ObjectId,
        ref: 'Market'
    },
    match: {
        type: Schema.Types.ObjectId,
        ref: 'Match'
    },
})

Market

const MarketSchema = new Schema({
    market: String,
    period: String,

    options: [
        {
            option: {
                type: Schema.Types.ObjectId,
                ref: 'Option'
            },
        }
    ],
})

Option

const OptionSchema = new Schema({
    name: String,
})

Here's how I'm currently populating the documents

let matches = await Match.find()
    .populate('home')
    .populate('away')
    .populate({
        path: 'competition',
        populate: [
            { path: 'country', model: 'Country' },
            { path: 'sport', model: 'Sport'}
        ]
    })
    .populate({
        path: 'odds',
        populate: [
            { path: 'option', model: 'Option' },
            { path: 'website', model: 'Website' },
            { path: 'market', model: 'Market' }
        ]
    })

I'm having issues populating the nested 'options' field in the populated market document.

I've tried chaining populate calls, like

.populate({
        path: 'odds',
        populate: {
            { path: 'market', model: 'Market', 
              populate: { path:'options.option', model: 'Option' }}
        }
    })

but I keep getting the same output of the Odd documents, where all fields are successfully populated except the options array in the nested market field.

odd output

Is there something wrong with the way I'm organising data? Am I calling populate incorrectly?
Thanks in advance

答案1

得分: 0

The options field in the Market model was not defined correctly. Should be

const MarketSchema = new Schema({
    market: String,
    period: String,

    options: [
        {
            type: Schema.Types.ObjectId,
            ref: 'Option'
        }
    ],
})
英文:

The options field in the Market model was not defined correctly. Should be

const MarketSchema = new Schema({
    market: String,
    period: String,

    options: [
        {
            type: Schema.Types.ObjectId,
            ref: 'Option'
        }
    ],
})

huangapple
  • 本文由 发表于 2023年5月17日 16:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定