When exploring MongoDB collections or analyzing their schema, you will stumble upon wrong field types.
Maybe it’s just a mistake on a single document, or maybe it’s across all documents in your collection, so you’ll need to bulk-edit.
You can save time by making these changes through Studio 3T, which lets you update any MongoDB field type – from Table View or Tree View – in just a few clicks.
Spot field type anomalies in your collection
Most of the time, we spot mistakes by coincidence.
A more systematic way of finding field type anomalies is to use Schema Explorer, which detects the type of each field in a collection.
Once connected to your database in Studio 3T, you’ll see the collections on the Connection Tree on the left.
Choose the collection you want to analyze, then click on the Schema button in the toolbar.
Choose to analyze All (documents). If you have a large collection, you can set it to the first, last, or random n documents.
Next, click on Run analysis.
You should see an overview of your collection’s fields, their field types, and their global probabilities (100% means all documents in the collection have this field).
Next, we can expand the individual fields to check. Let’s check the
dob (date of birth) field, which should definitely use the type
But it looks like 0.5% of them are saved as string values.
Right-click on the wrong field type (in this case,
string) and choose Explore documents.
This will lead us to a filtered view of the five documents containing the error. From here, we can quickly change field types in Table View or Tree View.
Repeat as needed for all fields in your collection. Next, we’ll show you how to change MongoDB field type in a single document or all documents.
Change the field type in a single document
In this example, we want to change the
dob field type from
date – but only in a single document.
Right-click on the field you want to edit (e.g.
dob) and choose Edit Value/Type.
Under Type, choose the new field type (e.g.
Then, choose to set the field in the current document only.
Click Set Value. This should update the MongoDB field type in that document.
Bulk-edit a field type in all documents in a collection
Another thing we spotted in our schema analysis is that the
transactions field – which stores a customer’s total number of transactions – is storing integers as
string values. Here the field type should be
Int32 (32-bit integer).
Right-click on any
transactions value and choose Edit Value/Type.
Under Type, choose
Then, choose to set the field in All documents in collection.
Next, make sure to choose Only change type (“Convert”).
Click on Set Value. This should update the field type across all documents in your collection.
Other field type editing options
Apart from editing a field type in a current document and all documents, you can also change the field type in:
- documents matching query criteria (here are four ways you can build a query in Studio 3T)
- documents matching query criteria that have a particular field
- all documents in collection that have a particular field
Follow the same steps as mentioned above – and make sure to only change type when editing multiple or all documents! 🙂