2

I have a schema like this:

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}]
});

When I do query on the collection, the field images would still show up even when I put the select: false. How can I hide the field without using .select('-images')?

4
  • Can you show us the query at hand? Commented Nov 7, 2016 at 10:23
  • City.find({}).sort('name').lean().exec(function(err, cities) { console.log(cities); }); Commented Nov 7, 2016 at 10:36
  • Have you tried City.find({}, 'name region').sort('name').lean().exec(function(err, cities) { console.log(cities); })? Commented Nov 7, 2016 at 10:46
  • Possible duplicate of Specifying select : false on array of subdocuments in Schema Commented Nov 7, 2016 at 11:07

1 Answer 1

1

When you are putting select: false, you say to exclude values inside the images array. You need to put select: false for the images array itself.

Look at this stackoverflow post.


Applied to your case:

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, 
    },
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.