Meteor update Collection with arrayFilters

Hi there,

I’m trying to resolve what part of functionality of updating nested arrays is working in Mongo.Collection just like in native MongoDB driver.
Is it possible to write that code using normal FlashcardCollection.update()?.

Meteor.methods({
  'flashcard.edit': async (id, {front, back}) => {
    FlashcardCollection.rawCollection().updateMany({}, {
      $set: {
        "flashcards.$[element].front": front,
        "flashcards.$[element].back": back
      }
    }, {
      arrayFilters: [
        {
          "element._id": id
        }
      ]
    })
  }
});

I get errors from SimpleSchema which are telling me that i’m trying to edit field flashcards.$[element].front so I assume that it’s not parsing the field correctly. On the other hand, above code works correctly.

I was reading Collections | Meteor API Docs and docs say that arrayFilter with filtered positional operator is present on Mongo.Collection class.
I would be glad for help.

Way easier https://docs.mongodb.com/manual/reference/method/Bulk/

I don’t get how it’s easier in such easy example. I’m new to Mongo.

It might not be supported by SimpleSchema. You can use the code above only with the danger that there is no schema validation happening

Ok bud well if you look at the manual you’ll see some examples on every method. Read that and you’ll be good to go.

Mongo supports doing multiple operations inside of a bulk transaction. It’s also very cpu efficient.

1 Like

Include “validate: false” inside your options:

return SomeCollection.update(
    { _id },
    {
        $set: {
            [`someArrayField.$[i].SomeNestedArrayField.$[j].someColumn`]: someNewValue
        },
    },
    {
        arrayFilters: [{ "i._id": someArrayFieldItemId }, { "j._id": someNestedArrayFieldItemId }],
        validate: false,
    }
);
2 Likes