1

I am trying to populate() all the subscriptions in my User model with data from the Show model. I have tried .populate('subscriptions.show') but it does nothing to the results.

If I make subscriptions a plain array of Refs like so

subscriptions: [{type: Schema.Types.ObjectId, ref: 'Show'}]

doing a populate('subscriptions') works as intended

I have looked at every similar question I could find on Stackoverflow and what I could find on the docs. I can't see what I am doing wrong.

complete test file source that i am working with https://gist.github.com/anonymous/b7b6d6752aabdd1f9b59

Schema and Models

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

var showSchema = new Schema({
  title: String,
  overview: String,
  subscribers: [{type: Schema.Types.ObjectId, ref: 'User'}],
  episodes: [{
    title: String,
    firstAired: Date
  }]
});

var User = mongoose.model('User', userSchema);
var Show = mongoose.model('Show', showSchema);

Initial Data

var user = new User({
  email: "test@test.com",
  displayName: "bill"
});

user.save(function(err, user) {
  var show = new Show({
    title: "Some Show",
    overview: "A show about some stuff."
  });

  show.save();
  user.subscriptions.push(show);
  user.save();
});

The Query

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

    console.log(user);
  });

results in:

{
  _id: 53a7a39d878a965c4de0b7f2,
  email: 'test@test.com',
  displayName: 'bill',
  __v: 1,
  subscriptions: [{
    _id: 53a7a39d878a965c4de0b7f3,
    favorite: false
  }]
}
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
m4tta
  • 43
  • 6
  • That post was not able to help me. Perhaps am missing what answer to my problem you see in there. Can you point out the relevant part? – m4tta Jun 23 '14 at 05:28
  • Check these links - 1. http://stackoverflow.com/questions/24245569/mongoose-populate-documents/24249211#24249211 2. http://stackoverflow.com/questions/24185367/mongoose-find-data-by-looping-on-an-array-of-models/24190334#24190334 – Harpreet Singh Jun 23 '14 at 05:31
  • None of these links help. The closest thing to what i am doing is this: http://stackoverflow.com/questions/14594511/mongoose-populate-within-an-object and i've tried doing what the accepted answer said. it has no affect. .populate('subscriptions.show') does not populate the show property on the subscriptions array – m4tta Jun 23 '14 at 06:44
  • Not a duplicate exactly as it is a little more involved. Provided are working sample for what you want – Neil Lunn Jun 23 '14 at 06:57

2 Answers2

1

Some code perhaps, also some corrections to your approach. You kind of want a "manyToMany" type of join which you can make as follows:

var async = require("async"),
    mongoose = require("mongoose"),
    Schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/user');


var userSchema = new Schema({
  email: String,
  displayName: String,
  subscriptions: [{ type: Schema.Types.ObjectId, ref: 'UserShow' }]
});

userShows = new Schema({
  show: { type: Schema.Types.ObjectId, Ref: 'Show' },
  favorite: { type: Boolean, default: false }
});

var showSchema = new Schema({
  title: String,
  overview: String,
  subscribers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  episodes: [{
    title: String,
    firstAired: Date
  }]
});


var User = mongoose.model('User', userSchema);
var Show = mongoose.model('Show', showSchema);
var UserShow = mongoose.model('UserShow', userShows);

var user = new User({
  email: 'test@test.com',
  displayName: 'bill'
});

user.save(function(err,user) {

  var show = new Show({
    title: "Some Show",
    overview: "A show about some stuff."
  });

  show.subscribers.push( user._id );
  show.save(function(err,show) {
    var userShow = new UserShow({ show: show._id });
    user.subscriptions.push( userShow._id );
    userShow.save(function(err,userShow) {
      user.save(function(err,user) {
        console.log( "done" );
        User.findOne({ displayName: "bill" })
          .populate("subscriptions").exec(function(err,user) {

          async.forEach(user.subscriptions,function(subscription,callback) {
              Show.populate(
                subscription,
                { path: "show" },
              function(err,output) {
                if (err) throw err;
                callback();
              });

          },function(err) {
            console.log( JSON.stringify( user, undefined, 4) );
          });


        });
      });
    });
  });

});

That should show a populated response much like this:

{
    "_id": "53a7b8e60462281231f2aa18",
    "email": "test@test.com",
    "displayName": "bill",
    "__v": 1,
    "subscriptions": [
        {
            "_id": "53a7b8e60462281231f2aa1a",
            "show": {
                "_id": "53a7b8e60462281231f2aa19",
                "title": "Some Show",
                "overview": "A show about some stuff.",
                "__v": 0,
                "episodes": [],
                "subscribers": [
                    "53a7b8e60462281231f2aa18"
                ]
            },
            "__v": 0,
            "favorite": false
        }
    ]
}

Or without the "manyToMany" works also. Note here that there is no initial call to populate:

var async = require("async"),
    mongoose = require("mongoose"),
    Schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/user');


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


var showSchema = new Schema({
  title: String,
  overview: String,
  subscribers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  episodes: [{
    title: String,
    firstAired: Date
  }]
});


var User = mongoose.model('User', userSchema);
var Show = mongoose.model('Show', showSchema);

var user = new User({
  email: 'test@test.com',
  displayName: 'bill'
});

user.save(function(err,user) {

  var show = new Show({
    title: "Some Show",
    overview: "A show about some stuff."
  });

  show.subscribers.push( user._id );
  show.save(function(err,show) {
    user.subscriptions.push({ show: show._id });
    user.save(function(err,user) {
        console.log( "done" );
        User.findOne({ displayName: "bill" }).exec(function(err,user) {

          async.forEach(user.subscriptions,function(subscription,callback) {
              Show.populate(
                subscription,
                { path: "show" },
              function(err,output) {
                if (err) throw err;
                callback();
              });

          },function(err) {
            console.log( JSON.stringify( user, undefined, 4) );
          });


        });
    });
  });

});
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Thank you taking the time to provide that working sample. I really didn't think this would require a 3rd model. I thought you could populate a reference nested in an object so i was forcing myself to get that to work. – m4tta Jun 23 '14 at 07:23
  • @LittleBill902 The reasoning is that populate keys off of the `_id` values in the related models. So your design here is a classic many to many. – Neil Lunn Jun 23 '14 at 07:27
  • @LittleBill902 Added you an example without the additional model relation – Neil Lunn Jun 23 '14 at 07:46
  • Thanks for that. I actually came to that conclusion with the new example you posted myself after examining your first solution. You have helped a lot. – m4tta Jun 23 '14 at 07:52
0

check answer: https://stackoverflow.com/a/28180427/3327857

  Car   .find()   .populate({
    path: 'partIds',
    model: 'Part',
    populate: {
      path: 'otherIds',
      model: 'Other'
    }   
 })
Community
  • 1
  • 1
Moshe Reubinoff
  • 187
  • 2
  • 8