How to updateMany many times within one function? MONGODB - Shell

38 views Asked by At

As the question suggests, I have multiple columns which I would like to rename via shell.

db.Hours.updateMany(
  {},
  {"$rename" : {"season" : "Season"}},
)
db.Hours.updateMany(
  {},
  {"$rename" :{"yr" : "Year"}}
)
db.Hours.updateMany(
  {},
  {"$rename" : {"cnt" : "TotalRiders"}}
)

Above is a snippet of the code, I believe it could be done by passing these arguements to one function. Is that possible? Something like below.

db.Hours."FUNCTION"(
{ { {}, {"$rename : { " " : " "}}},
  { {}, {"$rename : { " " : " "}}}....
})
1

There are 1 answers

2
Joe On

The $rename operator takes an object of containing oldName:newName fields.

You can pass more than one, like:

db.Hours.updateMany(
  {},
  {"$rename" : {"season" : "Season",
                "yr" : "Year",
                "cnt" : "TotalRiders"}}
)