Category: How do I do X?
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

Delete multiple selected rows with MongoDB

Issue

I am trying to create a multi-select delete documents query for a MongoDB database using checkboxes in a table. However, my current query only works for a single row. I need help with creating a query that can delete multiple rows at once.

Resolution

The goal was to create a multi-select delete query for a MongoDB database in Appsmith. The checkbox was added to a table, connected to a simple get documents query. The Appsmith syntax was unknown for deleting multiple rows. Initially, the selectedRow._id syntax was used to delete a single row, but that didn't work for multiple rows.

The first query that was tried using Table1.selectedRowIndices only worked for a single row. The second query that was tried using Table1.selectedRows and the map function showed only one _id instead of an array of them.

Finally, a solution was reached using a modified version of the second query. The modified query used the map function along with ObjectId() to convert the _ids to the ObjectId format that MongoDB requires. This query was able to delete multiple rows.

In summary, the solution involved using the map function to convert the _ids to the ObjectId format, and then using $in to match the resulting array of ObjectIds in the delete query.