Mongoose select:false不在位置嵌套对象上工作

Sca*_*aux 6 mongoose mongodb node.js mongoose-schema

我希望location默认情况下隐藏我的架构的字段.我添加了select: false属性,但在选择文档时总是返回...

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'});
Run Code Online (Sandbox Code Playgroud)

致电时:

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

输出是:

 {  
    cellphone: '+33656565656',
    location: { type: 'Point', coordinates: [Object] } <-- Shouldn't
 }
Run Code Online (Sandbox Code Playgroud)

编辑: 解释(感谢@alexmac)

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

ale*_*mac 4

您应该首先创建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
    }
});
Run Code Online (Sandbox Code Playgroud)