Mongoose select:false不适用于位置嵌套对象

我希望我的模式的location字段默认隐藏。 我添加了select: false属性,但在select文档时总是返回…

 var userSchema = new mongoose.Schema({ cellphone: { type: String, required: true, unique: true, }, location: { 'type': { type: String, required: true, enum: ['Point', 'LineString', 'Polygon'], default: 'Point' }, coordinates: [Number], select: false, <-- here }, }); userSchema.index({location: '2dsphere'}); 

打电话时:

User.find({ }, function(err, result){ console.log(result[0]); });

输出是:

  { cellphone: '+33656565656', location: { type: 'Point', coordinates: [Object] } <-- Shouldn't } 

编辑:解释(感谢@alexmac)

SchemaTypeselect选项必须应用于字段选项不是一个types。 在你的例子中,你已经定义了一个复杂types的位置,并将select选项添加到types。

您应该首先创buildlocationSchema ,然后使用模式typesselect: false

 var locationSchema = new mongoose.Schema({ 'type': { type: String, required: true, enum: ['Point', 'LineString', 'Polygon'], default: 'Point' }, coordinates: [Number] } }); var userSchema = new mongoose.Schema({ location: { type: locationSchema, select: false } });