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

Second level of populate doesn't work #5696

Closed
ghost opened this issue Oct 7, 2017 · 6 comments
Closed

Second level of populate doesn't work #5696

ghost opened this issue Oct 7, 2017 · 6 comments

Comments

@ghost
Copy link

ghost commented Oct 7, 2017

Do you want to request a feature or report a bug? (I don't know)
node version : 6.11.0
mongoose version : 4.11.13
mongodb : mLab

In Backend express.js:

populate "ingredients.DA01" doesn't work, populate "ingredients" (DA07RecIng) works, but the second level of populate (model "DA01Ingredient") in this exemple (picture above) doesn't work

Result in front-end:

Model from "DA02Recette" with a array "ingredients" to "DA07RecIng"

Model from "DA07RecIng"

Model from "DA01Ingredient"

Have you a trick to acomplish this ?

@sobafuchs
Copy link
Contributor

yeah, this is in the docs @stephanebressani, here's an example:

const mongoose = require('mongoose');
const co = require('co');
mongoose.Promise = global.Promise;
const GITHUB_ISSUE = `gh-5696`;


exec()
  .then(() => {
    console.log('successfully ran program');
    process.exit(0);
  })
  .catch(error => {
    console.error(`Error: ${error}\n${error.stack}`);
  });


function exec() {
  return co(function* () {
    yield mongoose.connect(`mongodb://localhost:27017/${GITHUB_ISSUE}`, { useMongoClient: true });

    const deepNested = new mongoose.Schema({ name: String });
    const DeepNested = mongoose.model('DeepNested', deepNested);

    const nested = new mongoose.Schema({
      deepNested: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'DeepNested'
      }
    });

    const Nested = mongoose.model('Nested', nested);

    const schema = new mongoose.Schema({
      nested: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Nested'
      }
    });
    const Model = mongoose.model('Model', schema);

    const dn1 = yield DeepNested.create({ name: 'test' });
    const n1 = yield Nested.create({ deepNested: dn1._id });

    const m1 = yield Model.create({ nested: n1._id });

    let doc = yield Model
      .findById(m1._id)
      .populate('nested'); // nested is populated
    console.log('doc with nested populate', doc);

    doc = yield Model
      .findById(m1._id)
      .populate('nested')
      .populate('nested.deepNested');
    console.log('doc with 2 populates', doc);

    doc = yield Model
      .findById(m1._id)
      .populate({ path: 'nested', populate: { path: 'deepNested' }});
    
    console.log('doc with correct populates', doc); // has deepNested populated
  });
}

@ghost
Copy link
Author

ghost commented Oct 9, 2017

Thank you ! it's works 👍

.populate({ path: 'nested', populate: { path: 'deepNested' }});

@sobafuchs
Copy link
Contributor

cool!

@vkarpov15
Copy link
Collaborator

Glad you found the issue 👍

@Spown thanks for your docs link but please be more respectful in the future 👍 🌴

@akritibhat
Copy link

submissionModel
.findById(sid)
.populate({ path: 'answers', populate: { path: 'question' }})
.exec()

not working

@b000cw
Copy link

b000cw commented Mar 30, 2021

Hi, I am able to get the deepNested, but in my Database I am having a newDeepNested inside deepNested like

const deepNested = new mongoose.Schema({ name: { type : String } , newDeepNest: { type: Schema.ObjectId, ref : "newDeepNest" }});

const newDeepNest = new mongoose.Schema({ name: String });

while using populate for nested how can I get the newDeepNest too.

@Automattic Automattic locked as resolved and limited conversation to collaborators Apr 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants