Unable to include and exclude fields of query via mongoose

mongodb, mongoose, node.js

Solution

This actually has nothing to do with mongoose. The "limitation" exists within mongo itself.

From the collection.find documentation

A projection cannot contain both include and exclude specifications, except for the exclusion of the `_id` field. In projections that explicitly include fields, the `_id` field is the only field that you can explicitly exclude.

You either include, or exclude, you can't do both, (with the exception of `_id`)

Select only `_id` and `one`

'one'

Select all fields except for `two`

'-two'

Exception: Select only `one` without `_id`

'one -_id'

Problem

I have some schema and making query as following : ``` mongoose.model('mymodel').find({}, 'one -two -__v', function(err,docs){}); ``` I want documents should only return the field 'one'. Always ignore the fields 'two' and rest all. Then i get the following error : ``` You cannot currently mix including and excluding fields. Contact us if this is an issue. ``` I am generating the field string dynamically to include or exclude the fields required. How to get rid of the error.

Original source