我希望默认情况下隐藏我的架构的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'});
致电:
User.find({ }, function(err, result){
console.log(result[0]);
});
输出为:
{
cellphone: '+33656565656',
location: { type: 'Point', coordinates: [Object] } <-- Shouldn't
}
编辑:说明(感谢@alexmac)
SchemaType select选项必须应用于字段选项而不是类型。在您的示例中,您已定义了复杂类型位置,并为类型添加了选项选项。
答案 0 :(得分:2)
您应首先创建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
}
});