Hi all! To anyone revisiting this thread to see how to perform multiple updates from a table on Retool to a collection of documents on MongoDB, I wanted to add some additional info. updateMany() works differently than a bulk update in postgres, as it's use case is limited to making one change for multiple documents. Let's say you wanted to change the status of a subset of records to "Complete", then updateMany() would be perfect for that scenario. It can change multiple fields, but it still has to be same change for all records that you're filtering over.
This is not likely to be useful in the context of updating a table in Retool, in which you may be trying to update the individual values of multiple fields in a table, referenced by table1.changesetArray for example. This is why Tess recommends looping over the array and performing an updateOne() for each row. This is perfectly good for a small amount of changes, but I should mention for larger updates (>5 at once) you should use a bulkWrite(). Here's an example of the syntax:
bulkWrite([
{
updateOne: {
filter: { _id: 1 },
update: { $set: { status: "active" } }
}
},
{
updateOne: {
filter: { _id: 2 },
update: { $set: { status: "pending" } }
}
}
])
Due to this formatting constraint, you can translate the info from a changesetObject, and you could provide the array of operations as follows:
{{ Object.entries(table16.changesetObject).map(row => (
{
updateOne: {
filter: {_id: {$oid: row[0]} },
update: {$set: row[1]}
}
}
)) }}
This would update all your rows in one query, rather than performing a separate query for each row ![]()