Mongoose: selecting all fields in a query (without knowing them at design-time) after having defined select: false in the schema

mongodb, mongoose

Solution

There is no better method in the 2.x branch. Schema.paths is not changing in 3.x either so its safe to do this for the foreseeable future.

3.x has new `select` syntax which allows for simply calling `factModel.findOne(..).select('+facts').exec(callback)` to achieve the desired result.

Problem

I'm using Mongoosejs to connect with MongoDB. Consider the following contrived example of a schema: ``` var factSchema= new Schema({ facts: { type:[require('./fact')], select:false} ,roles: {type: [String], required: true, index: {unique: false}} ,c: {type: {}, default: {}} //all calculated stuff based on facts } ``` Which explicitly states that when normally querying 'factSchema' the actual 'facts' aren't queried. Only after explicitly doing a select for 'facts' can I make sure those are included. I.e: ``` //factModel is a model derived from factSchema factModel.findOne({_id:input.id}) .select(["facts","roles","c"]).exec(function(err,result){//do something}); ``` This works in this trivial case. However I've subclassed factSchema (info on how: https://groups.google.com/forum/?fromgroups#!searchin/mongoose-orm/INHERIT/mongoose-orm/aeqGRRnpFvg/lbfIA54hiwYJ ) and I want to query all fields of a specific subclass, but it's only known at runtime which subclass I'm querying. In other words I can't explicitly specify the fields that I want to be returned. How would I go about this? A possible solution seems to be (?) that I can get to all the defined fields of a schema by doing `subclassedSchema.paths` where `subclassedSchema` is a subclass of `factSchema` . This works, but can I trust this to be stable under future releases? Any better, less hackish way to tackle this?

Original source