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

Mongoose select: false not working on location nested object

我希望默认隐藏架构的 location 字段。 我给它添加了 select: false 属性,但是当 selecting 文档时总是返回它...

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)

SchemaType select 选项必须应用于字段选项而不是类型。在您的示例中,您定义了一个复杂类型 Location 并向类型添加了 select 选项。

您应该首先创建 locationSchema,然后使用 select: 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
    }
});