What are the valid options for VirtualType

34 views
Skip to first unread message

Jason Cust

unread,
Mar 11, 2015, 7:44:40 PM3/11/15
to mongoo...@googlegroups.com
According to the documentation there is an optional options object (say that 10x fast) when creating a new virtual path: http://mongoosejs.com/docs/api.html#schema_Schema-virtual

What are the valid options for virtual paths? Are the similar to real path options?

I have been digging around trying to see if it is possible to do something akin to '{select: false}' with real paths for excluding virtual paths from the returned object unless explicitly asked for. I have tried to pass '{select: false}' as the optional options object but Mongoose doesn't seem to respect it if it is allowed.

Ryan Wheale

unread,
Mar 12, 2015, 5:01:08 PM3/12/15
to mongoo...@googlegroups.com
Not sure if you don't want prevent select it or if you don't want want it serialized when you go to send the data somewhere (eg. response.send()).  If the latter is the case, you can use the toObject method, which is self-documenting:

http://mongoosejs.com/docs/api.html#document_Document-toObject

doc.toObject({ getters: true, virtuals: false })

--
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/d/optout.

Ryan Wheale

unread,
Mar 12, 2015, 5:04:14 PM3/12/15
to mongoo...@googlegroups.com
Jeez, English is my first language, but you wouldn't guess it from my last email.  Should've read:

Not sure if you want to prevent virtual paths from being "selected" or if you want to prevent virtual paths from being serialized when you send the data somewhere...


Jason Cust

unread,
Mar 18, 2015, 8:43:44 PM3/18/15
to mongoo...@googlegroups.com
Ryan, thanks for the response. I was using `{select: false}` as an indicator if the options object was being respected or not. Apparently it's not.

Valeri Karpov

unread,
Mar 25, 2015, 1:59:51 PM3/25/15
to mongoo...@googlegroups.com
virtuals are explicitly not stored in the db, so select doesn't affect them. Beside the obvious get() and set(), I think the only tune-able parameter for virtuals is the aforementioned `virtuals` option to `toObject()` and `toJSON()` - if this option is true, the output from `toObject()` will include virtuals, otherwise it won't.
Reply all
Reply to author
Forward
0 new messages