gpt4 book ai didi

node.js - Mongoose 选择 : false not working on location nested object

转载 作者:IT老高 更新时间:2023-10-28 13:29:34 34 4
gpt4 key购买 nike

我希望我的架构的 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){
控制台.log(结果[0]);
});

输出是:

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

编辑:解释(感谢@alexmac)

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

最佳答案

您应该首先创建 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
}
});

关于node.js - Mongoose 选择 : false not working on location nested object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37115546/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com