question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Populate nested refObject in array

See original GitHub issue

Hi, I’m using 3.6.x version and here is my sample schema to reproduce :

var PersonSchema = new Schema({
  name    : String,
  stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

var StorySchema = new Schema({
  creator : { type: Schema.Types.ObjectId, ref: 'Person' },
  title    : String,
});

I’m trying to populate the creator ref for each stories saved, I tried the both path :

Person.findById(user1._id).populate("stories stories.creator"}).exec(function(err, doc)
Person.findById(user1._id).populate("stories stories.$.creator"}).exec(function(err, doc)

but I got the: TypeError: Cannot call method ‘path’ of undefined

My tests is running here :https://runnable.com/UT9iTD2wDj1UACXx

Thank you

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:6

github_iconTop GitHub Comments

9reactions
makrandguptacommented, Mar 4, 2018

Mongoose now supports deep populate. Example code:

var userSchema = new Schema({
  name: String,
  friends: [{ type: ObjectId, ref: 'User' }]
});

User.
  findOne({ name: 'Val' }).
  populate({
    path: 'friends',
    // Get friends of friends - populate the 'friends' array for every friend
    populate: { path: 'friends' }
  });
2reactions
skotchiocommented, Nov 3, 2013
Person.findById(user1._id).populate("stories")
    .exec(function(err, doc {
         Story.populate(doc.stories, {path: 'creator'}, function (err, doc) {

         })
     })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Populate nested array in mongoose - node.js - Stack Overflow
You can populate multiple nested documents like this. Project.find(query) .populate({ path: 'pages', populate: [{ path: 'components', model: 'Component' } ...
Read more >
How to populate nested document in MongoDB.
To populate nested documents you have to use . populate() method like above. I'm very beginner at MongoDB. Thanks.
Read more >
Mongoose: Problem populating nested array with aggregate
I've been trying to populate the user.profile.education array using aggregate . Particularly, the fields institution and major .
Read more >
Mongoose v6.8.2: Query Population
Arrays of refs work the same way. Just call the populate method on the query and an array of documents will be returned...
Read more >
Mongoose populate multiple levels | by Fred Wong - Medium
I have a Mongoose schema with an array lists of objects that consist of a reference to another collection and a nested… stackoverflow.com....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found