Mongodb Mongoose:在模式中定义select:false后,选择查询中的所有字段(在设计时不知道它们)

Mongodb Mongoose:在模式中定义select:false后,选择查询中的所有字段(在设计时不知道它们),mongodb,mongoose,Mongodb,Mongoose,我正在使用Mongoosejs连接MongoDB 考虑以下人为设计的模式示例: var factSchema= new Schema({ facts: { type:[require('./fact')], select:false} ,roles: {type: [String], required: true, index: {unique: false}} ,c: {type: {}, default: {}} //all calculated stuff bas

我正在使用Mongoosejs连接MongoDB

考虑以下人为设计的模式示例:

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
}
它明确指出,通常查询“factSchema”时,不会查询实际的“事实”。只有在明确地为“事实”进行选择之后,我才能确保包括这些事实。即:

 //factModel is a model derived from factSchema
 factModel.findOne({_id:input.id})
    .select(["facts","roles","c"]).exec(function(err,result){//do something});
这在这个小案例中有效。 然而,我已经对factSchema进行了子类化(关于如何查询的信息:),我想查询特定子类的所有字段,但只有在运行时才知道我查询的是哪个子类。换句话说,我不能明确指定要返回的字段

我该怎么办

一个可能的解决方案似乎是(?)我可以通过执行
subassessedschema.path
获得模式的所有定义字段,其中
subassessedschema
factSchema
的子类


这是可行的,但我能相信在未来的版本中它是稳定的吗?有没有更好的、不那么老练的方法来解决这个问题

在2.x分支中没有更好的方法。Schema.paths在3.x中也没有改变,因此在可预见的将来这样做是安全的

3.x有新的
select
语法,它允许简单地调用
factModel.findOne(..).select('+facts').exec(callback)
,以获得所需的结果