Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Reports
    • Case Studies
    • Whitepapers
    • Blog
    • Testimonials
    • Community
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Career
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • My License
  • Download
search

Studio 3T Knowledge Base

  • Documentation
  • Tutorials
  • Workshops
Take the fastest route to learning MongoDB. Cover the basics in two hours with MongoDB 101, no registration required.
Start the free course

The Quickest Way to Change a Field Type in MongoDB

Posted on: 10/06/2020 (last updated: 30/08/2021) by Kathryn Vargas

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:

  • Download the latest Studio 3T version.
  • Connect to your MongoDB database and open an existing collection. Don’t have one? Import this customers JSON file to follow along.
  • Get familiar with the BSON types supported by MongoDB.

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


Save time with other Studio 3T hacks for common MongoDB tasks:

  • How to rename fields in MongoDB
  • How to insert documents to a collection
  • How to limit MongoDB collection size

How helpful was this article?
This article was hideous
This article was bad
This article was ok
This article was good
This article was great
Thank you for your feedback!

About The Author

Kathryn Vargas

When she's not writing about working with MongoDB, Kathryn spends her free time exploring Berlin's food scene, playing the drums, learning languages (current mission: German), and hiking.

Article navigation

Related articles

  • Why can’t I change the _id field? #Studio3T_AMA
  • Exercise 3: Adding a computed ratio field based on the converted lookup field
  • Is there a quicker way to query _id values? #Studio3T_AMA
  • What is the fastest way to make a database connection? #Studio3T_AMA
  • Better MongoDB Field Comparisons With Tables #Studio3T_AMA

Studio 3T

MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

Education

  • Free MongoDB Tutorials
  • Connect to MongoDB
  • Connect to MongoDB Atlas
  • Import Data to MongoDB
  • Export MongoDB Data
  • Build Aggregation Queries
  • Query MongoDB with SQL
  • Migrate from SQL to MongoDB

Resources

  • Feedback and Support
  • Sales Support
  • Knowledge Base
  • FAQ
  • Reports
  • Case Studies
  • White Papers
  • Testimonials
  • Discounts

Company

  • About Us
  • Blog
  • Careers
  • Legal
  • Press
  • Privacy Policy
  • EULA

© 2022 3T Software Labs GmbH. All rights reserved.

  • Privacy Policy
  • Cookie settings
  • Impressum
When you click "Accept", you are agreeing to cookies being on your device. They may improve site navigation, site usage analysis, or the relevance of messages. It is up to you which cookies are enabled. Read our Privacy Policy.
Manage cookies
Accept
✕

Privacy Preference Center

A cookie is a small file of letters and numbers that is downloaded on to your computer when you visit a website. Cookies are used by many websites and can do a number of things, eg remembering your preferences, recording what you have put in your shopping basket, and counting the number of people looking at a website. In the form below you can choose which cookies, except for essential cookies, to allow or disable.

Accept all cookies

Manage consent preferences

Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

Accept selected