Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deep population (>4 levels) #3973

Closed
TrejGun opened this issue Mar 13, 2016 · 4 comments
Closed

deep population (>4 levels) #3973

TrejGun opened this issue Mar 13, 2016 · 4 comments
Milestone

Comments

@TrejGun
Copy link
Contributor

TrejGun commented Mar 13, 2016

isn't population work recursively?
i expected to populate 4 levels deep but found out its impossible
the 4th level isn't getting populated

    it.only('4 levels population', function (done) {
      var level4Schema = new Schema({
        name: {type: String}
      });

      var level3Schema = new Schema({
        name: {type: String},
        level4: [{type: Schema.Types.ObjectId, ref: 'level_4'}]
      });

      var level2Schema = new Schema({
        name: {type: String},
        level3: [{type: Schema.Types.ObjectId, ref: 'level_3'}]
      });

      var level1Schema = new Schema({
        name: {type: String},
        level2: [{type: Schema.Types.ObjectId, ref: 'level_2'}]
      });

      var level4 = db.model('level_4', level4Schema);
      var level3 = db.model('level_3', level3Schema);
      var level2 = db.model('level_2', level2Schema);
      var level1 = db.model('level_1', level1Schema);

      level4.create([
        {name: 'level 4'}
      ], function (error, l4) {
        level3.create([
          {name: 'level 3', level4: l4[0]._id}
        ], function (error, l3) {
          assert.ifError(error);
          level2.create([
            {name: 'level 2', level3: l3[0]._id}
          ], function (error, l2) {
            assert.ifError(error);
            console.log(l2)
            level1.create([
              {name: 'level 1', level2: l2[0]._id}
            ], function (error, l1) {
              assert.ifError(error);
              level1.findById(l1[0]._id)
                  .populate({
                    path: 'level2',
                    populate: {
                      path: 'level3',
                      populate: {
                        path: 'level4'
                      }
                    }
                  })
                  .exec(function (error, obj) {
                    assert.ifError(error);
                    var util = require('util');
                    console.log(util.inspect(obj, {depth: 10}));
                    assert.ok(obj);
                    done();
                  });
            });
          });
        });
      });
    });

this should not be a big problem to put code in a function and call it recursively for each next level

@vkarpov15 vkarpov15 added this to the 4.4.8 milestone Mar 17, 2016
@vkarpov15
Copy link
Collaborator

vkarpov15 commented Mar 17, 2016

Womp. Quite possibly the case, deep populate has a lot of rough edges, as you've noticed. I'll try reproing in a bit.

@vkarpov15
Copy link
Collaborator

Another case of not getting the model correctly. As a workaround:

.populate({
                    path: 'level2',
                    populate: {
                      path: 'level3',
                      populate: {
                        path: 'level4', model: 'level_4' // <-- explicitly tell mongoose to use this model
                      }
                    }
                  })

vkarpov15 added a commit that referenced this issue Mar 18, 2016
@TrejGun
Copy link
Contributor Author

TrejGun commented Mar 19, 2016

great news! thanks
i will tests and report of any problems on my projects

@vkarpov15
Copy link
Collaborator

Please do, thanks for finding all the bugs :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants