model populate and lean

327 views
Skip to first unread message

Edu

unread,
Dec 4, 2013, 5:35:19 PM12/4/13
to mongoo...@googlegroups.com
Hi

Is it possible to use model populate and lean()?

I've read on the documentation that

Model.findOne().lean().populate(whatever).exec(...) 

the result will be a plain javascript object, right?

but, how can I use the lean option on model populate? for instance:


User.populate(user,{path:'messages'},function(err,user){...}); // I want user to be lean

help please!

Nik Martin

unread,
Dec 6, 2013, 7:51:17 PM12/6/13
to mongoo...@googlegroups.com
A few ways:
1: 
inside your callback, do:
user = user.toObject();

user is now a plain javascript object.

2: on your schema, do:
schema.set('toObject', { getters: true });

and ALL objects returned from the schema will be plain objects.

Edu

unread,
Dec 8, 2013, 3:18:24 PM12/8/13
to mongoo...@googlegroups.com

Hi Nik,

I need lean() option in order to avoid casting to MongooseDocuments and increase performance.

I'm afraid that with your solution what the code would do is cast to MongooseDocument and then cast to javascript Object. Am I right?

Is there any way to telling mongoose to do a lean populate?

thanks!

Aaron Heckmann

unread,
Dec 16, 2013, 7:41:54 PM12/16/13
to mongoo...@googlegroups.com
User.populate(user, { path: '..', options: { lean: true}},
cb)

Need to update the docs.
--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
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.


--

Edu

unread,
Dec 17, 2013, 2:23:28 PM12/17/13
to mongoo...@googlegroups.com
oh, awesome

thanks Aaron!


El martes, 17 de diciembre de 2013 01:41:54 UTC+1, Aaron Heckmann escribió:
User.populate(user, { path: '..', options: { lean: true}},
cb)

Need to update the docs.

On Wednesday, December 4, 2013, Edu wrote:
Hi

Is it possible to use model populate and lean()?

I've read on the documentation that

Model.findOne().lean().populate(whatever).exec(...) 

the result will be a plain javascript object, right?

but, how can I use the lean option on model populate? for instance:


User.populate(user,{path:'messages'},function(err,user){...}); // I want user to be lean

help please!

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
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+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages