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: "[email protected]",
  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: '[email protected]',
  displayName: 'bill',
  __v: 1,
  subscriptions: [{
    _id: 53a7a39d878a965c4de0b7f3,
    favorite: false
  }]
}
4
  • 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? Commented Jun 23, 2014 at 5:28
  • Check these links - 1. stackoverflow.com/questions/24245569/… 2. stackoverflow.com/questions/24185367/… Commented Jun 23, 2014 at 5:31
  • None of these links help. The closest thing to what i am doing is this: stackoverflow.com/questions/14594511/… 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 Commented Jun 23, 2014 at 6:44
  • Not a duplicate exactly as it is a little more involved. Provided are working sample for what you want Commented Jun 23, 2014 at 6:57

2 Answers 2

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: '[email protected]',
  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": "[email protected]",
    "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: '[email protected]',
  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) );
          });


        });
    });
  });

});
Sign up to request clarification or add additional context in comments.

4 Comments

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.
@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.
@LittleBill902 Added you an example without the additional model relation
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.
0

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

  Car   .find()   .populate({
    path: 'partIds',
    model: 'Part',
    populate: {
      path: 'otherIds',
      model: 'Other'
    }   
 })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.