population and deep paths (3.6.0rc0)

2,268 views
Skip to first unread message

Marco Kaiser

unread,
Mar 5, 2013, 5:22:19 AM3/5/13
to mongoo...@googlegroups.com
Hi,

I've been trying to get nested population work in 3.6.0rc0, but without luck.

I have a Schema A which has a collection of references to Schema B, and Schema B has a reference to a single document of Schema C:


var SchemaA = new Schema({ bs: [ { type: Schema.Types.ObjectId, ref: 'B' } ] });
var SchemaB = new Schema({ c: { type: Schema.Types.ObjectId, ref: 'B' } });
var SchemaC = new Schema({ ... });


var ModelA = mongoose.model('A', SchemaA);
var ModelB = mongoose.model('B', SchemaB);
var ModelC = mongoose.model('C', SchemaC);


 Now, I want to findOne of A, then populate the array of B in that one, and also the Cs in all the Bs. That's what I am trying:


ModelA.findOne({ ... }).populate('bs').populate('bs.c').exec( ... );


But I get an exception:


/home/marco/code/app/node_modules/mongoose/lib/model.js:1830
      foundschema
= schema.path(trypath);
                           
^
TypeError: Cannot call method 'path' of undefined
    at search
(/home/marco/code/app/node_modules/mongoose/lib/model.js:1830:28)
    at search
(/home/marco/code/app/node_modules/mongoose/lib/model.js:1849:22)
    at
Function._getSchema (/home/marco/code/app/node_modules/mongoose/lib/model.js:1856:5)
    at populate
(/home/marco/code/app/node_modules/mongoose/lib/model.js:1594:22)
    at
Function.Model.populate (/home/marco/code/app/node_modules/mongoose/lib/model.js:1573:5)
    at model
.populate (/home/marco/code/app/node_modules/mongoose/lib/document.js:256:22)
    at
Promise.WantController.show (/home/marco/code/app/routes/want.js:21:11)
    at
Promise.onResolve (/home/marco/code/app/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
    at
Promise.EventEmitter.emit (events.js:96:17)
    at
Promise.emit (/home/marco/code/want/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)


Any suggestions or hints at what I am doing wrong? Unfortunately, there isn't much documentation for this new feature yet...

Thanks in advance! 

Aaron Heckmann

unread,
Mar 5, 2013, 12:39:17 PM3/5/13
to mongoo...@googlegroups.com
Populating references spanning more than two collections must be done manually using Model.populate(docs, options, callback).

ModelA.findOne({ ... }).populate('bs').exec(function (err, doc) {
  ModelB.populate(doc, 'c', function (err, doc) {
    console.log(doc.bs.c)


--
--
http://mongoosejs.com - docs
http://plugins.mongoosejs.com - plugins search
http://github.com/learnboost/mongoose - source code
http://mongoosejs.tumblr.com/ - production examples
 
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en
---
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/groups/opt_out.
 
 



--
Aaron


Marco Kaiser

unread,
Mar 6, 2013, 9:23:49 AM3/6/13
to mongoo...@googlegroups.com
Thanks Aaron, that works!

Marco

Felipe Lorenzo VI

unread,
May 14, 2013, 11:09:04 AM5/14/13
to mongoo...@googlegroups.com
Hi,

What if I want to populate ModelB from find({...}) which returns an array instead of findOne()?

Aaron Heckmann

unread,
May 15, 2013, 12:34:20 PM5/15/13
to mongoo...@googlegroups.com
Model.populate supports arrays too.



---
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/groups/opt_out.
 
 

styb...@gmail.com

unread,
Jul 25, 2014, 5:40:25 PM7/25/14
to mongoo...@googlegroups.com
How do you use field selection on the second populate?


ModelA.findOne({ ... }).populate('bs').exec(function (err, doc) {
  ModelB.populate(doc, 'c', '_id', function (err, doc) {
    console.log(doc.bs.c)

Doesnt work.

On Tuesday, March 5, 2013 6:39:17 PM UTC+1, Aaron Heckmann wrote:
Populating references spanning more than two collections must be done manually using Model.populate(docs, options, callback).

ModelA.findOne({ ... }).populate('bs').exec(function (err, doc) {
  ModelB.populate(doc, 'c', function (err, doc) {
    console.log(doc.bs.c)

Anthony Ettinger

unread,
Jul 25, 2014, 5:52:28 PM7/25/14
to mongoo...@googlegroups.com
This is how i do it. use an object { path: 'creator', select: '-salt -hashedPassword' }


newJob.save(function (err, job) {
                 if (err) return res.json(400, err);

Job.populate(job, { path: 'creator', select: '-salt -hashedPassword -email' }, function(err, job){
job = utils.getJobPerms(job, user);
job = utils.job.getCreatorProfile(job);

res.json(job);
})
    });


---
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.
Reply all
Reply to author
Forward
0 new messages