MongoDB Update Many Example

Hi - I'm trying to work with MongoDB UpdateMany method but cannot seem to get my table.recordUpdates structured correctly.

Can someone provide a simple example of how to do this?

{
  $set: {
    {{tableSettings.recordUpdates}}
  },
  upsert:true,
  multi:true
}

Hey @theleadingpractice

A lot of users actually use a JS loop & an updateOne query type.

You'll start with a JS query that maps through your .recordUpdates and triggers a new updateOne query for each row (You'll replace id and sales with the values in your table. You'll also want to replace table5 and updateOne with your table name & update query name):

Then, you create an updateOne query that has dynamic values for each value from your recordUpdates:

Let me know if you think this would be helpful for your use case!

By this answer do you mean updateMany does not work with MongoDB? If it does can you provide an example, I'm also trying to get this to work

Hi @voboxjohn

It does work in Retool. For example, this query updates all sales in the users table to be equal to 10

Let us know if you're seeing any errors with your query

Can you provide an example on how to update the whole row? I'm trying to use table.recordUpdates[0] to update the db with table values.

However, it would require me to remove the _id from the object.

Performing an update on the path '_id' would modify the immutable field '_id'

I'd like to know how to do this right from the Update.

image

Hi @yeh Thanks for reaching out. If you only need to reference recordUpdates[0], you could do something like this (using _.omit()):

However, I'd typically recommend following the JS query mapped approach:

Query1:

Query2:

1 Like

Thanks a lot!

Hi!

Sorry Tess, I have a doubt, your JSCode return a query3, whats this? where you do define it?

Otherwise what I want to do is to manually edit the fields in the table and when I save them, update fields on my MongoDB table.

Hi @Raul_Vazquez_Mendez thanks for checking in! Realizing my post was a bit confusing.

The Mongo query (in my third screenshot above and posted again below) is named "query3"

So the JS query shown in my previous message triggers this query and passes in dynamic values for the _id, _name, and number: