Delete multiple selected rows with MongoDB

So I’m trying to create a multi-select delete documents query for a MongoDB database. I’ve added the checkboxes to a table that is connected to a simple get documents query.

Desired outcome:
I check several boxes and click a button. Upon clicking the button it fires the delete query, deleting all the selected rows from the MongoDB database.

What I have in a Raw query:

{
      "delete": "workouts",
      "deletes": [ { 
            "q": { _id: ObjectId("{{Table1.selectedRow._id}}") }, 
            "limit": 0
      } ]
   }

This works great for a single row, but I just don’t know the appsmith syntax to have this query delete multiple rows. Is it a map function? I tried “selectedRows” as well, but my guess is some additional code would be needed for that. My js skills are getting better, but are still quite lacking.

Any help would be great!

@drew Did you try Table1.selectedRowIndices ?

Saw that in the documentation, not sure how to implement it to delete MongoDB documents.

@drew Try this query

{
      "delete": "workouts",
      "deletes": [ { 
      "q": { _id: { $in: [ {{(Table1.selectedRows.map(item => "ObjectId('" + item._id + "')")).toString()}}]}},
      "limit": 0
      } ]
}

If the above does not work, also try this.

{
      "delete": "workouts",
      "deletes": [ {
      "q": { _id: { $in: [ {{(Table1.selectedRows.map(item => "\\"ObjectId('" + item._id + "')\\"")).toString()}}]}},
      "limit": 0
      } ]
}

@dancia The first query seems close. It still only works on a single row though. And according to the “evaluated value” preview, it’s only showing one _id instead of an array of them.

@drew The one below appears to work. Can you try and let me know?

{
      "delete": "workouts",
      "deletes": [ {
      "q": { _id: { $in: {{(Table1.selectedRows.map(item => item._id)).map((id) => `ObjectId("${id}")`)}}}},      
      "limit": 0
      } ]
}

@dancia Yep! That did the trick, thanks a bunch!