New DBRef-Functionality:How do you Populate DBRefs in Embedded Documents?

639 views
Skip to first unread message

chrisgl

unread,
Aug 26, 2011, 6:29:45 AM8/26/11
to Mongoose Node.JS ORM
Hello!
First of all! The new DBRef-Function with Populate is great! It works
very smooth.
But I found a problem, which is not described in the Examples.
I have a Schema with one Populated Field creator which resolves to the
User schema. This works fine.
There is also an Array of embedded Documents (comments) which has a
field createdby, also resolving to User schema.
The Exampleschema is below. If I try to populate the Embedded
document's ref it fails!
My Query looks like this:
Schema.findOne({titel:id}).populate('creator').populate('comments.createdby').run(function(err,
result){});

But the Callback is never called. So I get no Answer from my page!
So my Question is: How do you Populate DBRefs in Embedded Documents?

Exampleschema:
var CommentSchema = new Schema({
text: String,
date: {type: Date, "default": Date.now},
createdby: {type: ObjectId, ref:'user'}
});

var ExampleSchema = new Schema({
titel: {type: String, unique: true},
created: {type: Date, "default": Date.now},
creator: {type: ObjectId, ref:'user'},
comments: [CommentSchema]
});

Thanks in advance!

Aaron Heckmann

unread,
Aug 26, 2011, 10:42:28 AM8/26/11
to mongoo...@googlegroups.com
here is a similar working example: https://github.com/LearnBoost/mongoose/blob/7ae5a82352f5239316ceba49fabd5f8337cc30cd/test/model.ref.test.js#L513-549

Are you executing the query on the Model or the Schema?

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
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



--
Aaron


chrisgl

unread,
Aug 26, 2011, 7:42:07 PM8/26/11
to Mongoose Node.JS ORM
I am executing it on the model. I mixed it up above. So it should work
the way I thought. I will investigate further on Sunday.
Thanks for the link to the example.

On 26 Aug., 16:42, Aaron Heckmann <aaron.heckm...@gmail.com> wrote:
> here is a similar working example:https://github.com/LearnBoost/mongoose/blob/7ae5a82352f5239316ceba49f...

chrisgl

unread,
Sep 6, 2011, 8:06:20 AM9/6/11
to Mongoose Node.JS ORM
Again thanks for the help.
Just wanted to share with you, that it is working now.

Model.findOne({titel:id}).populate('creator').populate('comments.createdby').run(function(err,
result){...});

This works!
Reply all
Reply to author
Forward
0 new messages