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

Usage of arrayfilters with discriminator : no element updated #12565

Closed
1 task done
BTpro opened this issue Oct 18, 2022 · 0 comments · Fixed by #12802
Closed
1 task done

Usage of arrayfilters with discriminator : no element updated #12565

BTpro opened this issue Oct 18, 2022 · 0 comments · Fixed by #12802
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@BTpro
Copy link

BTpro commented Oct 18, 2022

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.3.1

Node.js version

16.13.1

MongoDB version

5.0.9

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Issue

Hi to everyone,

I have a problem with the filters of discriminators. I have read issues which looked like mine #9977 and tested several solutions but it is not working.

I want to push an element in an array property of discriminator and for that I need to filter with his type (the discriminator key) and his number.

Despite my tests, my new object is never added to my parent object.

Please find below the example I use:
VersionSchema contains an Array of object which are using discriminatorKey

My test model for a diagram :

const elementSchema = new Schema(
  {
    elementType: {
      type: String,
      required: true,
    },
  },
  {
    discriminatorKey: "elementType",
    timestamps: true,
  }
);

const versionSchema = new Schema(
  {
    elements: [elementSchema],
  },
  {
    timestamps: true,
  }
);

versionSchema.path("elements").discriminator(
  "Graph",
  new Schema(
    {
      number: {
        type: String,
        required: true,
      },
      curves: [curveSchema],
    },
    {
      timestamps: true,
    }
  )
);

Hereafter my test service to push a new item in my Array of elements using the discriminator (I cleaned the code, so I might not be accurate concerning the brackets). The aim here is to push a new subelement of an existing element in the array of the parent element. I need to use an array filter ($and) to find the element using the discriminator and to find the element with a specific number inside. Looks like the results get no answer for the update. I manually check, and the is one candidate for the update. What am I doing wrong here ?

  test.findOneAndUpdate(
    {
      label: "diag01",
    },
    {
      $push: {
        "versions.$[version].elements.$[element].curves": {
          line: {
            label: "CC110_Ligne 02",
            type: "numerique",
            number: "30",
            latLong: [44, 8],
            controller: "CC110",
          },
        },
      },
    },
    {
      new: true,
      arrayFilters: [
        { "version.version": "1.0" },
        {
          $and: [{ "element.number": "1" }, { "element.elementType": "Graph" }],
        },
      ],
    },
    }
  );
}

@BTpro BTpro added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Oct 18, 2022
@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 Oct 24, 2022
@vkarpov15 vkarpov15 assigned vkarpov15 and unassigned IslandRhythms Nov 2, 2022
@vkarpov15 vkarpov15 added this to the 6.7.4 milestone Nov 2, 2022
@vkarpov15 vkarpov15 removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Nov 2, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.7.4, 6.8, 6.7.5 Nov 22, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.7.6, 6.7.7 Dec 4, 2022
@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 Dec 5, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.7.7, 6.7.6 Dec 5, 2022
vkarpov15 added a commit that referenced this issue Dec 19, 2022
fix(update): handle embedded discriminators when casting array filters
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
Development

Successfully merging a pull request may close this issue.

3 participants