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

Reference subdocument is not getting populated with subdocument which is also in same collection #3244

Closed
sham332 opened this issue Aug 8, 2015 · 3 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@sham332
Copy link

sham332 commented Aug 8, 2015

Below is the schema design code

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;
var ObjectId = mongoose.Schema.Types.ObjectId;

mongoose.set('debug', true);

var chapter = new Schema({
  title:String,
  startPage:Number,
  endPage:Number
});
mongoose.model('chapter', chapter);

var keyPoint = new Schema({
  text:String,
  mentionInChapter:[{type: Schema.ObjectId, ref: 'chapter'}]
});
mongoose.model('keyPoint', keyPoint);

var Book = new Schema({
   title:String,
   author:String,
   chapters:[chapter],
   keyPoints:[keyPoint]
});

mongoose.model('Book', Book);

And when I try to retrieve Book populated with all values, I get blank array. Below is express module containing the query

    exports.getCompleteBook = function(req,res){
        var bookId = '55c65c8749286ac0e25d1f58';
        Book.findById(bookId)
        .lean() 
        .populate({ path: 'keyPoints' }) 
        .exec(function (err, docs) {
            if (err) throw err;
            console.log(docs);
            Book.populate(docs, {path: 'keyPoints.mentionInChapter', model : 'chapter', select:'title'}, function (err, book) {
              if (err) throw err;
              res.json(book);
            });
        });
    };

This link to jist has further on data etc, https://gist.github.com/sham332/20ad1c7b1b460c478700

I should have recieved the 'mentionInChapter' subdocument populated with values, but it is coming blank.

@vkarpov15 vkarpov15 modified the milestones: 4.1.3, 4.1.4 Aug 10, 2015
@vkarpov15
Copy link
Collaborator

What you're gonna want is:

var Book = new Schema({
   title:String,
   author:String,
   chapters:[{type: Schema.ObjectId, ref: 'chapter'}],
   keyPoints:[{type: Schema.ObjectId, ref: 'keyPoint'}]
});

If you're embedding subdocs, you're not going to be able to run populate() on the array, because the subdocs are stored in the doc itself rather than in a separate collection.

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Aug 24, 2015
@vkarpov15 vkarpov15 removed this from the 4.1.4 milestone Aug 24, 2015
@ogheneovo12
Copy link

what if the subdoc contains a reference to an item in another collection,
take for a example a cart with a subdoc of CartItem that references Product in another Collection.

@vkarpov15
Copy link
Collaborator

That's fine, as long as Product is a Mongoose model. We have a sample app with a very similar setup to what you're describing, a cart with an array of subdocuments with a productId property that points to a Product model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

3 participants