Skip to content

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

Closed
@vkarpov15

Description

@vkarpov15

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions