16

I'm using mongoose.js on a node.js server connecting to mongodb and I have a mongoose model like the following

SubSchema = new Schema({
    _member:     {type: ObjectId, ref: 'Member'},
    members:     [{type: ObjectId, ref: 'Member'}],
    created:     { type: Date, default: Date.now }
});
mongoose.model('SubModel', SubSchema);

MainSchema = new Schema({
    _member:     {type: ObjectId, ref: 'Member'},
    subs:        [SubSchema],
    members:     [{type: ObjectId, ref: 'Member'}],
    created:     { type: Date, default: Date.now }
});
var MainModel mongoose.model('MainModel', MainSchema);

which i pull with a command like this

var q = MainModel.find({})
                 .sort('created', -1)
                 .limit(25)
                 .populate("_member")
                 .populate("subs._member")
                 .populate("subs.members");

q.execFind(function(err, mains){
    //mains[0].subs[0].members - will always be empty why?
});

my problem is that i can't get subs.members array to populate or even load, it just keeps showing as an empty array.

I've tried .populate("subs.members") to no avail even though subs._member loads just fine

2
  • 1
    I am looking for the solution to this as well, my subdocs two levels deep will not populate. Did you ever find one?
    – 371273
    Commented Nov 27, 2012 at 3:19
  • 3
    This is a widely requested feature that is expected soon. github.com/LearnBoost/mongoose/issues/601
    – JohnnyHK
    Commented Nov 27, 2012 at 3:30

5 Answers 5

16

try this

    SubSchema = new Schema({
        _member:     {type: ObjectId, ref: 'Member'},
        members:     [{type: ObjectId, ref: 'Member'}],
        created:     { type: Date, default: Date.now }
    });
    var SubModel = mongoose.model('SubModel', SubSchema);//add

    MainSchema = new Schema({
        _member:     {type: ObjectId, ref: 'Member'},
        subs:        [SubSchema],
        members:     [{type: ObjectId, ref: 'Member'}],
        created:     { type: Date, default: Date.now }
    });

    var MainModel = mongoose.model('MainModel', MainSchema);

    MainModel.find({})
             .sort('created', -1)
             .limit(25)
             .populate("_member")
             .populate("subs._member")
             .exec(function(err, mains){

                 //add
                 SubModel.populate(mains,{
                     path:'subs.members'
                 },function(err,mains){
                    //mains[0].subs[0].members - is not empty
                 });
             });
1
  • works, most complete answer.. and it did take bug fixes and workarounds from the mongoose team to get to this point, although original issue never was fixed.. someone did come up with a workaround to make it work as well... gist.github.com/joeytwiddle/6129676
    – chrishawn
    Commented Feb 25, 2014 at 16:53
7

@leesei: I can't comment on your post (too little rep), so I leave this as a separate answer.

In mongoose 3.6 subdoc population still doesn't work, the issue github.com/LearnBoost/mongoose/issues/1381 has been closed 7 months ago with the following solution as a workaround. I had to change it slightly to merge the populated subdocument back to the main document.

The subdocument's model Story has to be specified explicitly:

Person.findById(user1._id).populate("stories")
    .exec(function(err, doc {
         Story.populate(doc.stories, {path: 'creator'}, function (err, stories) {
             doc.stories = stories;
             return doc;
         })
})

In the solution above this works:

Story.populate(doc.stories, {path: 'creator'}, callback)

but this still won't work:

Story.populate(doc, {path: 'stories.creator'}, callback)
1

Follow-up on @JohnnyHK's post, you can specify the Model to use in populate() for now:

https://github.com/LearnBoost/mongoose/issues/1377#issuecomment-15920370

0

I had several nest layers deep of sub docs, and none of the supplied options worked for me. I found this amazing Mongoose plugin that will do deep population seamlessly. You just use the same syntax you would expect to work with populate, but it actually works.

https://github.com/buunguyen/mongoose-deep-populate

-1

I have something that looks a slightly different but populates the document with the array items. I'm wondering if it's the objectid's that are causing the issues.

var mongoose = require('mongoose');
var Schema = mongoose.Schema, ObjectID = Schema.ObjectId;

var SubSchema = new Schema({
    testsub: String,       
    created: { type: Date, default: Date.now }
});

var MainSchema = new Schema({
    test: String
    subs: [SubSchema],
    created: { type: Date, default: Date.now }
});

mongoose.model('MainSchema', MainSchema, mainschema);

var query = MainSchema.find({});
3
  • I changed up my example. Hope it helps you out. Commented Oct 11, 2012 at 18:52
  • 1
    yeah this exmaples is still one level less complex.. my array on mainschema always populates fine.. the array on the array of subschema is the one that never populates... i don't think it is the objectid because the objectid ref on the subitem not in an array popuates fine
    – chrishawn
    Commented Oct 12, 2012 at 16:29
  • Ah. Gotcha. What does your Member schema look like? Commented Oct 12, 2012 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.