'Cannot do exclusion on field in inclusion projection' error when field1 excluded in schema & query.select('field2 -_id') #12670
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Prerequisites
Mongoose version
6.7.2
Node.js version
18.x
MongoDB server version
5.x
Typescript version (if applicable)
No response
Description
I would like to report a long standing bug regarding simultaneous field exclusion in schema and select() query, with -_id not specified as first field.
Steps to Reproduce
Suppose we have field1 excluded from selection/projection in schema:
The error returned is
Cannot do exclusion on field field1 in inclusion projection
.However, if I first exclude the _id in the select() then the query executes just fine, no error in that case:
Not sure when this regression was introduced or whether it has ever worked in the first place. @vkarpov15 could you please double check? Thanks for looking into this!
Expected Behavior
Excluding -_id from select() should work irrespective of its position in the list of fields. At the moment it only works when -_id is the first field, then the rest of the included fields.
This issue only occurs when there's another field excluded from select in the schema.
The text was updated successfully, but these errors were encountered: