## Strapi Rename Field in Mongo Database

_August 13th 2020_

We recently ran into a situation where we needed to rename a field in our Strapi content type from `time_start` to `time_min` but we had already created documents (records) in our Mongo Database for this content type that we need to preserve and we need to preserve the data. We were looking for a solution to change the field in our collection content type while also maintaining the data in the database. To prevent data loss, Strapi currently creates a new field in your database instead of overwriting the current field:

> Strapi does not rename or drop anything it will only create at this time. Migrations need to be done manually.

This quick tutorial will walk you through updating a field in Strapi while preserving the data in your database.

### Updating the Model Definition

In order to update the name of the field you will have to drill down into your `api/<content-type>/models/<content-type>.settings.json` file (replacing `<content-type>` with your content type and find the field in the `attributes` key that you want to edit. In our case, the field was named `time_start`.

```
"attributes": {
    "slug": {
      "type": "uid",
      "targetField": "name",
      "required": true
    },
    "time_start": {
      "type": "integer"
    },
}
```

Since we didn’t need to change the `type` of the field all we need to do here is rename the `time_start` key to `time_min` such that:

```
"attributes": {
    "slug": {
      "type": "uid",
      "targetField": "name",
      "required": true
    },
    "time_min": {
      "type": "integer"
    },
}
```

This tells Strapi that our field is now `time_min` however, this will create a new field in our Mongo database and the data in our mongo documents will still be left within the `time_start` field that we previously defined. We will need to manually update this field in our Mongo Database and rename all instances of `time_start` to `time_min`.

### Renaming a Field in MongoDB

Fortunately, for us Mongo shell provides a quick and easy solution to rename all instances of a field in our mongo database. In this instance the `$rename` operator during an `update()` will be the solution of choice!

> The [`$rename`](https://docs.mongodb.com/manual/reference/operator/update/rename/#up._S_rename) operator updates the name of a field and has the following form:
>
> [https://docs.mongodb.com/manual/reference/operator/update/rename/](https://docs.mongodb.com/manual/reference/operator/update/rename/)

```
{$rename: { <field1>: <newName1>, <field2>: <newName2>, ... } }
```

Considering our example we would need to update the collection (the name of our content-type) including all of the documents and renaming the field `time_start` to `time_min`. In our case our collection name is `workouts` so our command in our mongo shell would look like:

```
db.workouts.updateMany( {}, { $rename: { 'time_start': 'time_min' } } )
```

The `updateMany` will update the documents that meet the selector (the first argument in our `updateMany` method which in our case is an object so all of the documents will be updated. We are then using the `$rename` operator to rename the field from `time_start` to `time_min` which matches the name in our `api/workout/models/workout.settings.json` attributes field that we changed in the previous step.

### Quick Summary

In order to change the field name in out Strapi content type and preserve the data we need to update your `api/<content-type>/models/<content-type>.settings.json` file to change the name of the field. Then we need to login to the mongo shell and use the `updateMany` method on our collection using the `$rename` operator to rename all instances of the field to the new name we defined in our model’s JSON settings.
