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

Model.hydrate() should work with subdocument array projection #14680

Closed
2 tasks done
lcrosetto opened this issue Jun 21, 2024 · 0 comments · Fixed by #14686
Closed
2 tasks done

Model.hydrate() should work with subdocument array projection #14680

lcrosetto opened this issue Jun 21, 2024 · 0 comments · Fixed by #14686
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@lcrosetto
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.4.3

Node.js version

20.14.0

MongoDB server version

7.0.11

Typescript version (if applicable)

No response

Description

When calling Model.hydrate() with a schema that includes a subdocument array, and passing a projection as the second argument, this projection does not get applied correctly to the subdocuments. The result is an array of empty objects in the subdocument array field.

I believe this might be related to the behavior of applyInclusiveProjection(), which is recursive and will call itself with the subdocument array as the first argument.

Steps to Reproduce

The following snippet will reproduce this behavior:

      const embedSchema = new mongoose.Schema({
        field1: String,
      });
      const testSchema = new mongoose.Schema({
        testField: {
          type: String,
          required: true,
        },
        testArray: [embedSchema],
      });
      const TestModel = mongoose.model('Test', testSchema);
      const obj = {
        _id: new mongoose.Types.ObjectId(),
        testField: 'foo',
        testArray: [{_id: new mongoose.Types.ObjectId(), field1: 'bar'}],
      };
      const res = TestModel.hydrate(obj, {'testArray.field1': true});

The result is an object that looks like:

{ testArray: [ {} ] }

The first level projection is applied correctly, but all keys in the subdocument have been deleted.

Expected Behavior

If a schema with an array of subdocuments is passed, the projection should be applied as expected, or throw an error if unsupported.

@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jun 24, 2024
vkarpov15 added a commit that referenced this issue Jun 25, 2024
fix(projection): handle projections on arrays in `Model.hydrate()` projection option
@vkarpov15 vkarpov15 added this to the 8.4.4 milestone Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
2 participants