【发布时间】:2016-09-04 01:48:12
【问题描述】:
我希望我的架构的 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 选择选项必须应用于字段选项而不是类型。在您的示例中,您已经定义了一个复杂类型 Location 并向类型添加了选择选项。
【问题讨论】:
-
findOne呢,它会返回还是不返回?
-
问题已解决。是的,findOne 也返回了它:)
-
如果您分享您的解决方案会很好。 @MalteseFalcon
标签: node.js mongodb mongoose mongoose-schema