Try Studio 3T for Free
  1. Knowledge Base
  2. MongoDB Tutorials
  3. How to Change a Field Type in MongoDB

How to Change a Field Type in MongoDB

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.

Prerequisites:

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.

Already know what to fix? Skip to the next section.

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.

Click on the Schema button to start analyzing MongoDB field types

Choose to analyze All (documents). If you have a large collection, you can set it to the first, last, or random n documents.

Choose to analyze all 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).

Overview of our MongoDB collection's schema

Next, we can expand the individual fields to check. Let’s check the dob (date of birth) field, which should definitely use the type date.

But it looks like 0.5% of them are saved as string values.

String values in dob field

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.

Schema analysis results in Table 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 string to date – but only in a single document.

Right-click on the field you want to edit (e.g.dob) and choose Edit Value/Type.

Edit field type/value

Under Type, choose the new field type (e.g. date).

Then, choose to set the field in the current document only.


Change MongoDB field type from string to date

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.

Editing MongoDB field types, this time transactions

Under Type, choose Int32.

Then, choose to set the field in All documents in collection.

Next, make sure to choose Only change type (“Convert”).

Bulk-edit the field type in all documents in a collection

We are only changing field types – not values – so make sure to choose “Only change type” here. Choosing “Always change value” will update the value (in this case, to 59) across all documents, which we absolutely do not want!

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! 🙂


Updated on June 19, 2020

Was this article helpful?

Related Articles

Leave a Comment