-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MongoError: Projection cannot have a mix of inclusion and exclusion. #7017
Comments
Does it work if you don't pretend + to the path? + is special and select('+c') is slightly different from select('c') |
I got the same error. it happened when i tried to exclude a filed that popped up in my results due to a |
@Wim-mie the below script works as expected, no errors. Please modify the below script to demonstrate your issue. const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);
const GITHUB_ISSUE = `gh7017`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
await mongoose.connect(connectionString);
await mongoose.connection.dropDatabase();
const schema = new Schema({
field1: { type: String, select: false },
foo: String
});
const Model = mongoose.model('Test', schema);
await Model.create({ field1: 'test', foo: 'test' });
const doc = await Model.findOne().select('+field1 -_id -foo');
console.log(doc);
} |
If you have used Mongo Projection or Project so might be you have added that key name those are not in the document. |
I want to report a bug, unfortunately I was not totally able to reproduce the original case I have, but stumbled on something simliar that could be related.
What is the current behavior?
If you have a model that has some fields unselected by default, and explicity selecting another one that is not part of the definition (this is the test case, in my original case the defintion is part of it, but I guess it is related to the other bug where mongoose can not correctly read the type definition #7016 ) the query fails with projection cannot have a mix of inclusion and exlusion.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
The expected behavior is that this query still runs, without a mix up in the projection. I would assume as soon as I specify a "+" select, mongoose does not add any "-" select to the query.
Please mention your node.js, mongoose and MongoDB version.
mongoose 5.2.15, v8.11.4, mongodb 3.6
The text was updated successfully, but these errors were encountered: