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

Array field UpdateMany - $set as object instead of $push array #402

Open
meabed opened this issue Apr 17, 2022 · 0 comments
Open

Array field UpdateMany - $set as object instead of $push array #402

meabed opened this issue Apr 17, 2022 · 0 comments

Comments

@meabed
Copy link
Contributor

meabed commented Apr 17, 2022

Thank you so much ❤️ for all the great work and effort.

I have found a potential bug:

When you use updateMany resolver on array field it is executed on mongoose as $set.0 - $set.1 - .... and this result of the mongodb field data type to become Object instead of Array

For example

# mongoose 
const userSchema = new Schema(
  {
    companies: {
      type: [Schema.Types.ObjectId],
      ref: 'Company',
    }
});

schemaComposer.Mutation.addFields({
  userUpdateMany: userTC.mongooseResolvers.updateMany(),
});

# graphql
mutation updateUser {
  userUpdateMany(
    filter: { email: "[email protected]" }
    record: { companies: ["625b27bdada2c1e426941971", "625b27bdada2c1e426941971"] }
  ) {
    numAffected
  }
}
# the result query is like this
Mongoose: users.updateMany({ email: '[email protected]' }, { '$setOnInsert': { createdAt: new Date("Sun, 17 Apr 2022 01:55:38 GMT") }, '$set': { 'companies.0': new ObjectId("625b27bdada2c1e426941971"), 'companies.1': new ObjectId("625b27bdada2c1e426941971"), updatedAt: new Date("Sun, 17 Apr 2022 01:55:38 GMT") }}, { limit: 100, multi: true })

So the companies field becomes object because it's first time set.
image

Ideally, updateMany should $set the whole array not the indexes. so the query should be like

users.updateMany({ '$set': { 'companies': [new ObjectId("625b27bdada2c1e426941971")] } });

Using UpdateOne works as expected - this issues is only with updateMany.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant