How to rename a field in MongoDB using MongoChef

Every now and then you may need to change the name of a field within your MongoDB documents. 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 the MongoChef app.

Renaming A Field Name In A Single Document Within MongoDB

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

  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

MongoChef Support, MongoChef, MongoDB

Renaming A Field Name 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:

  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”

MongoChef Support, MongoChef, MongoDB

Renaming A Field Name 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):

  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”

MongoChef Support, MongoChef, MongoDB

 

That’s it for this post, but if you still have a little time, please check out the other very useful features of MongoChef!