Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

node.js - Mongoose deep populate is not returning the populated document that has a ref on itself

I have a simple setup similar to the below:

const mongoose = require('mongoose');

const schema1Schema = new mongoose.Schema({
    schema2Items: [
        {
            schema2Item: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'schema2s',
            },
        },
    ],
});

const schema2Schema = new mongoose.Schema({
    name: {
        type: String,
    },
    selfReferences: [
        {
            selfReference: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'schema2s',
            },
        },
    ],
});

module.exports = {
    Schema1: mongoose.model('schema1s', schema1Schema),
    Schema2: mongoose.model('schema2s', schema2Schema),
}

Running find on Schema2 returns the following,

const schema2Documents = await Schema2.find().select('-_id -__v')

[
    { name: 'schema2 One', "selfReferences": [] },
    { name: 'schema2 Two', "selfReferences": [
        { "name": 'schema2 Three', "selfReferences": [] },
        { "name": 'schema2 Four', "selfReferences": [] },
    ] },
    { name: 'schema2 Five', "selfReferences": [] },
]

But if I run find or findOne and populate on Schema1, it returns this:

const schema2ItemsInSchema1 = await Schema1.findOne().populate('schema2Items.schema2Item').select('-_id -__v')

{
    "schema2Items": [
        {
            "schema2Item": {
                "name": "schema2 One",
                "selfReferences": []
            }
        },
        {
            "schema2Item": {
                "name": "schema2 Two",
                "selfReferences": [
                    {}, // <-- is not being populated
                    {} // <-- is also not being populated
                ]
            }
        },
        {
            "schema2Item": {
                "name": "schema2 Five",
                "selfReferences": []
            }
        }
    ]
}

Why is the selfReferences field in schema2Items.schema2Item path not being populated? The expected result should be,

const schema2ItemsInSchema1 = await Schema1.findOne().populate('schema2Items.schema2Item').select('-_id -__v')

{
    "schema2Items": [
        {
            "schema2Item": {
                "name": "schema2 One",
                "selfReferences": []
            }
        },
        {
            "schema2Item": {
                "name": "schema2 Two",
                "selfReferences": [
                    { "name": 'schema2 Three', "selfReferences": [] },
                    { "name": 'schema2 Four', "selfReferences": [] },
                ]
            }
        },
        {
            "schema2Item": {
                "name": "schema2 Five",
                "selfReferences": []
            }
        }
    ]
}

Edit

I've tried the following,

const schema2ItemsInSchema1 = await Schema1.findOne().populate({
            path: 'schema2Items.schema2Item',
            model: 'schema2s',
            populate: { path: 'selfReferences', model: 'schema2s' },
        }).select('-_id -__v')

But still no luck. I've also tried mongoose-autopopulate, and that didn't work either.

question from:https://stackoverflow.com/questions/65883662/mongoose-deep-populate-is-not-returning-the-populated-document-that-has-a-ref-on

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...