populating array of objects with a ref property

2,340 views
Skip to first unread message

Matt

unread,
Jun 23, 2014, 3:02:41 AM6/23/14
to mongoo...@googlegroups.com
How can I populate the show property in a Schema like this:

var userSchema = new Schema({
  email: String,
  displayName: String,
  subscriptions: [{
    show: {type: Schema.Types.ObjectId, ref: 'Show'},
    favorite: {type: Boolean, default: false}
  }]
});

I've tried something like this

User.findOne({displayName: 'bill'})
  .populate('subscriptions.show')
  .exec(function(err, user) {
    console.log(user);
  });

It does nothing to populate it. I get something like this returned

{
  _id: 53a7a39d878a965c4de0b7f2,
  email: 'te...@test.com',
  displayName: 'bill',
  __v: 1,
  subscriptions: [{
    _id: 53a7a39d878a965c4de0b7f3,
    favorite: false
  }]
}

Anthony Ettinger

unread,
Jun 23, 2014, 1:53:58 PM6/23/14
to mongoo...@googlegroups.com
For deep populations you have to manually do it with the model object:


User.findOne({displayName: 'bill'})
  .exec(function(err, user) {
  User.populate(user, 'subscriptions.show', function(err, user){
   console.log(user);
  });
  });


--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Anthony Ettinger
http://about.me/anthony.ettinger
ant...@ettinger.name
+1 (831) 406-1123
Reply all
Reply to author
Forward
0 new messages