Closed eyal88 closed 5 years ago
Mongoose automatically projects in populated fields, including ones this plugin populates. Try this: A.findOne().select('name -type')
Thanks @vkarpov15.
You'r right about the automatic inclusion of populated fields.
Omitting a field like '-type'
does work, however it can't be done with an explicit inclusion of the 'name'
field. That's where the error came from.
From mongoose docs:
A projection must be either inclusive or exclusive. In other words, you must either list the fields to include (which excludes all others), or list the fields to exclude (which implies all other fields are included). The _id field is the only exception because MongoDB includes it by default.
https://mongoosejs.com/docs/api.html#query_Query-select
Thanks again!
Setting
autopopulate: true
to a field in a schema makes it being projected automatically. Trying to explicitly omit it with:{ "field": 0 }
in the query results in an error:Demo
Schema A
Schema B
Init
Queries
Query 1
Query 2
Actual result
Query 1
Query 2
Projection cannot have a mix of inclusion and exclusion.
Expected result