0

Hye!

This is an example of http://mongoosejs.com/docs

var personSchema = Schema({
   _id     : Number,
   name    : String,
   age     : Number,
   stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title    : String,
fans     : [{ type: Number, ref: 'Person' }]
});
var Story  = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);

Story
.findOne({ title: 'Once upon a timex.' })
.populate('_creator')
.exec(function (err, story) {
if (err) return handleError(err);
console.log('The creator is %s', story._creator.name);
// prints "The creator is Aaron"
})

And that is my codeMy '_creator' is not an array but an array of objects :

var storySchema = Schema({
   _creator : [{
       _id : { type: Number, ref: 'Person' }, 
       quantity : Number
   }],
   ...
});

And my request is :

Story
.findOne({ title: 'Once upon a timex.' })
.populate('_creator._id')
.exec(function (err, story) {
if (err) return handleError(err);
console.log('The creator is %s', story._creator.name);
// prints "The creator is Aaron"
})

But this solution 'populate('_creator._id')' doesn't work.

Have you got an idea?

Thank you!

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
  • populate a sub document: http://stackoverflow.com/questions/13026486/how-to-populate-a-sub-document-in-mongoose-after-creating-it – damphat Jan 14 '14 at 15:06
  • I have read others post, but for me it doesn't work.. [http://stackoverflow.com/questions/14594511/mongoose-populate-within-an-object?rq=1][1] [1]: http://stackoverflow.com/questions/14594511/mongoose-populate-within-an-object?rq=1 – user3194401 Jan 16 '14 at 09:16

0 Answers0