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

select('name -path') should resolve to select('name') if path has schema-level select: true #11694

Closed
vkarpov15 opened this issue Apr 20, 2022 · 0 comments

Comments

@vkarpov15
Copy link
Collaborator

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

There's currently no way to exclude fields that have schema-level select: true in an inclusive projection. Using -path throws an error:

'use strict';
  
const mongoose = require('mongoose');

const { Schema } = mongoose;

run().catch(err => console.log(err));

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

  await mongoose.connection.dropDatabase();

  const schema = new Schema({
    name: String,
    age: { type: String, select: true }
  });
  const Test = mongoose.model('Test', schema);

  await Test.create({ name: 'Jean-Luc Picard', age: 59 });

  // Throws "MongoServerError: Cannot do exclusion on field age in inclusion projection"
  const doc = await Test.findOne().select('name -age');
  console.log(doc);
}

We have tests in schema.select.test.js that assert on this behavior, so we can't change this until the next major release. We went most of the way in #11546, which allows using -__t to exclude the discriminator key.

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

@vkarpov15 vkarpov15 added this to the 7.0 milestone Apr 20, 2022
vkarpov15 added a commit that referenced this issue Jan 12, 2023
vkarpov15 added a commit that referenced this issue Jan 19, 2023
Allow deselecting fields where select is set to true in the schema definition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant