如何用mongoose排除数组类型字段

时间:2016-11-07 10:22:42

标签: node.js mongodb mongoose

我有这样的架构:

var CitySchema   = new Schema({
    name: {type : String, required : true},
    region: {type: Schema.Types.ObjectId, ref: 'Region', required : true},
    images: [{type : Schema.Types.ObjectId, ref: 'Image', select: false}]
});

当我对集合进行查询时,即使我放置select: false,现场图像仍会显示。如何在不使用.select('-images')的情况下隐藏字段?

1 个答案:

答案 0 :(得分:1)

当您放置select: false时,您说要排除 images数组中的值。您需要为select: false数组本身添加images

请看this stackoverflow post

适用于您的案例

var CitySchema   = new Schema({
    name: {type : String, required : true},
    region: {type: Schema.Types.ObjectId, ref: 'Region', required : true},
    images: { 
      type: [{type : Schema.Types.ObjectId, ref: 'Image', select: false}],
      select: false, 
    },
});