Skip to content
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

Closed
simllll opened this issue Sep 16, 2018 · 4 comments
Closed

MongoError: Projection cannot have a mix of inclusion and exclusion. #7017

simllll opened this issue Sep 16, 2018 · 4 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@simllll
Copy link
Contributor

simllll commented Sep 16, 2018

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.

const mongoose = require('mongoose');

console.log(mongoose.version);

run().catch(error => console.error(error.stack));

async function run() {
    await mongoose.connect('mongodb://localhost:27017/test3');

    const ModelA = mongoose.model('ModelA', new mongoose.Schema({
        a: {
            select: false,
            type: {
                _reference: {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: 'ModelB'
                },
            }
        },
        b: { select: false, type: String }
    }));

    const result = await ModelA.find({}, '+c');

    console.log('result', JSON.stringify(result, false, 3));
}

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

@vkarpov15
Copy link
Collaborator

Does it work if you don't pretend + to the path? + is special and select('+c') is slightly different from select('c')

@vkarpov15 vkarpov15 added this to the 5.2.18 milestone Sep 23, 2018
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Sep 23, 2018
vkarpov15 added a commit that referenced this issue Sep 25, 2018
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Sep 25, 2018
@ghost
Copy link

ghost commented May 17, 2019

I got the same error. it happened when i tried to exclude a filed that popped up in my results due to a ClientSchema.set( 'toJSON', { getters: true } );.
It still happened when I removed the toJSON option for the getters, and forgot to remove the exclusion. Once I removed that exclusion (but kept the -_id exclusion) it worked fine.
(tried several ways: select('+field1 -_id -getter-to-exclude'), select({field:1, _id:0, getter-to-exclude:0}), find({_id: req.params.clientId}, {field:1, _id:0, getter-to-exclude:0}), all resulting in same error).
version: mongoose 5.2.15, nodeJS 10.15.1, MongoDB 4.0.2

@vkarpov15 vkarpov15 reopened this May 18, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.2.18, 5.5.11 May 18, 2019
@vkarpov15 vkarpov15 removed the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 18, 2019
@vkarpov15
Copy link
Collaborator

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

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label May 22, 2019
@vkarpov15 vkarpov15 removed this from the 5.5.11 milestone May 22, 2019
@neeltiwari
Copy link

If you have used Mongo Projection or Project so might be you have added that key name those are not in the document.
Remove projection key and check it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

3 participants