Search code examples
javascriptnode.jsmongodbmongoosemongoose-populate

Mongoose deep populate results in [Object] at 2nd level


After spent 1 day trying to get things done, I think it's time to ask for help.

I want to populate 2 levels of a document.

I tried with populate() but it seems to work only for first level, not deep populate! I read a lot at SO and I know it should work but I think I'm missing something really stupid...

Please let me know where I'm making mistakes.

Here are the relevant code.

Schemas

var compositionSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    contributions: [{
        type: Schema.Types.ObjectId,
        ref: 'Contribution'
    }]
});
mongoose.model('Composition', compositionSchema);

var contributionSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    media: {
        type: Schema.Types.ObjectId,
        ref: 'Media'
    }
});
mongoose.model('Contribution', contributionSchema);

var mediaSchema = new Schema({
    name: {
        type: String,
        required: true
    }
});
mongoose.model('Media', mediaSchema);

Actual documents saved in MongoDB

compositions:

{
    "_id" : ObjectId("59e5db4595fe650a71fb0e07"),
    "name" : "Test Pop 7",
    "contributions" : [ 
        ObjectId("59e5db4595fe650a71fb0e05")
    ]
}

contributions:

{
    "_id" : ObjectId("59e5db4595fe650a71fb0e05"),
    "name" : "Honda",
    "media" : ObjectId("59e4ac5dacacd709eac2c856")
}

media:

{
    "_id" : ObjectId("59e4ac5dacacd709eac2c856"),
    "name" : "Logo_big.png",
    "width" : 662,
    "height" : 540
}

My tries (= the wrong code?)

In Node JS, when I do this (as per documentation):

Composition.findOne({ name: "Test Pop 7" })
        .populate({
            path: 'contributions',
            model: 'Contribution',
            populate: {
                path: 'media',
                model: 'Media',
            }
        })
        .exec((error, doc) => {
            if (error) { console.log(error); }
            else {
                console.log(doc);
            }
        });

prints out this, without actually populate the media field:

{ _id: 59e5db4595fe650a71fb0e07,
  name: 'Test Pop 7',
  contributions: 
   [ { _id: 59e5db4595fe650a71fb0e05,
       name: 'Honda',
       media: [Object] } ]
}

Solution

  • It works, keeping in mind the key-word in your question: prints. Printed, with console.log(), it just shows you the type (checked with typeof) of the document included in an array for some (2nd) level of nesting. If you do:

    console.log(doc.contributions[0].media[0])
    

    you will see your populated media document.