How can we rename a field or give alias to field in MongoDB Atlas charts?

215 Views Asked by At

How can we rename a field or give alias to field in MongoDB Atlas charts?

1

There are 1 best solutions below

2
On

Examples: this is the students collection

{
  "_id": 1,
  "alias": [ "The American Cincinnatus", "The American Fabius" ],
  "mobile": "555-555-5555",
  "nmae": { "first" : "george", "last" : "washington" }
},
{
  "_id": 2,
  "alias": [ "My dearest friend" ],
  "mobile": "222-222-2222",
  "nmae": { "first" : "abigail", "last" : "adams" }
}

Here I update the nmae field to name

db.students.updateMany( {}, { $rename: { "nmae": "name" } } )

{
  "_id": 1,
  "alias": [ "The American Cincinnatus", "The American Fabius" ],
  "mobile": "555-555-5555",
  "name": { "first" : "george", "last" : "washington" }
},
{
   "_id" : 2,
   "alias" : [ "My dearest friend" ],
   "mobile" : "222-222-2222",
   "name" : { "first" : "abigail", "last" : "adams" }
}

ref: https://docs.mongodb.com/manual/reference/operator/update/rename/