mongoose-lean-defaults icon indicating copy to clipboard operation
mongoose-lean-defaults copied to clipboard

[BUG] Wrong projection when a field starts with another field name

Open throskam-wooclap opened this issue 1 year ago • 0 comments

Describe the bug

The projection returns unselected fields when they start with another field name.

For instance, if we have the following schema

const schema = new Schema({
  myField: String,
  myFieldButLonger: String,
});

Then a query like this

Model.find().select({ myField: 1 });

Would return both fields.

To Reproduce
Steps to reproduce the behavior:

  • Create a schema with 2 fields with the same prefix
  • make a request with the shorter field name selected

Expected behavior

Only have the default value on the selected field and keep other fields as undefined.

Additional context

Mongoose version: ^6.0.0 Node version: latest

throskam-wooclap avatar Feb 02 '24 12:02 throskam-wooclap