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.

Icon representing the update actionYou 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:

Selected rows are shown as a query selecting objectids of documents
Selected rows are shown as a query selecting object ids of 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:

Selecting a field generates an update for that field
Selecting a field generates an update for that specific field

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.

Interface Switch for Selected Document
Selected Documents is one of three query modes in the Update Dialog

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.

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href=""> <abbr> <acronym> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>