1. Knowledge Base
  2. Studio 3T Tutorials
  3. How to Rename a MongoDB Field

How to Rename a MongoDB Field

Every now and then, you may need to rename MongoDB fields.

Whether you need to do it for a single document, or perhaps for multiple documents, this task can be easily accomplished with only a few steps from within Studio 3T.

Rename MongoDB field in a single document

The simplest of these tasks is changing a field name in single document. See the steps below:

By editing a document via JSON, you can quickly rename fields. Just double click the field to be changed and begin typing.

  1. Open the document in which the field to be renamed is located
  2. Double click on the field name (In this case “dob”)
  3. You will now have the option to type the new field name into this space
  4. Once you press “Enter” the new field name will be saved

Rename MongoDB field within documents matching a query

Should you wish to change all documents matching a particular query then this can also be easily achieved in just a few steps. In this example we are changing the “dob” field in all documents relating to customers with the last name “Miller”. See below:

Right-click and go to 'Rename Field', then choose 'Documents matching query criteria'. This lets you update the field name for all results matching query criteria.

  1. Type in your query: {last:"Miller"} – and hit “Enter”
  2. Open one of the documents resulting from the query search
  3. Find the field that you would like to rename (“dob”) and right click it.
  4. Select “Rename Field”
  5. Type in the new field name (“date_of_birth”) and hit “Enter”
  6. Select “Documents matching query criteria” from the dropdown box
  7. Click “Enter”
  8. Now every document within this query will have the field name “date_of_birth” in place of “dob”

Rename MongoDB field in all documents within a collection

Finally, to rename a field in all documents within a collection follow the steps below (identical to the steps above apart from no. 4):

Right click a field and go to 'Rename Field'. From the drop down menu, rename the field and from the drop-down menu choose 'All documents in collection'. This will then update the field name for all documents in the collection.

  1. Find the field that you would like to rename (again “dob”) and right click it
  2. Select “Rename Field”
  3. Type in the new field name (“date_of_birth”) and hit “Enter”
  4. Select “All documents in collection” from the dropdown box
  5. Click “Rename”
  6. Now every document in the entire collection will have the field name “date_of_birth” in place of “dob”

Check out other ways you can edit documents quickly and easily in Studio 3T:

Updated on April 5, 2019

Was this article helpful?

Related Articles

Leave a Comment