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:

Hi @Tess, I try use your example, but don't work for me



Screenshot 2023-04-19 at 5.39.28 PM

retool say success but it is not reflected in the database

Hi @osirisfrik Hmm, that is strange :thinking: From what I can tell, this looks to be set up correctly. Are you triggering this on the save changes button in the table?

Can we expand those items in the Retool debug console to see the exact values that are sent from saveChanges to updateOne? (i.e. how was additionalScope defined)

I'd also be curious if hardcoding some values works -- just to confirm there isn't a syntax issue

For example, changing {{_id}} to a hardcoded _id & changing {{readableCode}} to a hardcoded value

HI @Tess, I found an error in the js code (additionalScoope -> additionalScope), I have already solved it but the problem persists

Screenshot 2023-04-24 at 4.45.45 PM

Oh right! Thanks for calling that out. I think we're working on some linting to make that more noticeable. Hmm, if you put those values into the updateOne query by hardcoding them, does it also not update?

How are you confirming that the values aren't updating? Are you calling a get request in Retool?

Hello, I have tried these steps but my mongo db instance still doesn't reflect the changes. Also the read lines beneath the params you are passing, isn't that the sign of an error. Please forgive me, I am new to Retool @Tess

Hi @oyinda_david can you share some screenshots? The mongo query will have red lines for the params you're passing in because the values are not defined yet. They'll be defined when you run the JS query

Hello @Tess thank you for your explanation. I didnt understand the earlier explanations, but its clear now. I have been able to update multiple rows in the mongoDB

Oh, wonderful! :tada: Welcome to Retool, btw! Let us know if other questions come up