Smart and Safe MongoDB Multi-Document Updates #Studio3T_AMA
Q: How do I do updates of multiple unrelated documents in our database, safely.
Welcome to the latest Studio 3T Ask Manatees Anything – #Studio3T_AMA. Want to know how to use Studio 3T to make your MongoDB life better? Just tweet and use #Studio3T_AMA or email [email protected].
A: In-place editing of field values is useful in any tool. But when there are multiple documents to be modified it can get tedious and that tedium can let errors creep in.
Say you are looking at the documents in the Studio 3T Table view and you can see the ones you want to change. In this example, they have nothing in common so writing a simple query isn’t going to help.
What you need to do is use the multiple document selector; command-click (Mac) or control-click(Windows/Linux) to select the documents you want to include in your update. If there’s one particular field you want to change, click on that field when selecting the rows.
You are now ready to call on the Update dialog. Click on the icon or use the context menu item or take a shortcut with Command/Alt U):
The Update dialog is where you can create an update but to make things quicker it has some predefined sources for its query. One of those is Selected Documents:
If you have anything selected in the table, Selected Document(s) is automatically applied. It takes the unique id for each row and builds a query for you that only selects those particular rows.
Now it’s a matter of switching tabs to the Update tab where you can see the update that’s going to be performed:
If you clicked on a cell before calling up the Update dialog, then you’ll be in luck as Studio 3T will have created a simple update command with '$set'
for you. Edit the field’s value in the '$set' command and click Update to apply that change to all the selected fields.
You can add fields to that '$set' clause if you want to do more extensive changes.
Not just for selections
The Update dialog isn’t just for handling multiple selections though. That’s just one of the smart defaults that’s built into Studio 3T. For example, If you have created a query using the Query field or the Visual Query Builder, you can use the Update dialog with that query too.
For any query, bring up the Update dialog. The first thing you will see is your query in the Query tab. If you don’t, it may be because you selected a row in the table which triggers the Selected Rows behavior we saw earlier. Just change the Apply Predefined Query setting from “Selected Rows" to Search Query from Collection View.
The Update dialog's Query will then inherit the View’s query. You are ready to create your Update. This time the update will apply to documents matching the query not selected rows.
And there you have it, the Update dialog is your power tool for changing data, from discrete and different rows to bulk updates on documents matching a query.