0

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 : { " " : " "}}}....
})
BoogieMan
  • 71
  • 1
  • 1
  • 6

1 Answers1

0

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"}}
)

Joe
  • 25,000
  • 3
  • 22
  • 44
  • How would you do this for a situation where ```db.Hours.updateMany( {"Season" : "1"}, { "$set" : { "Season" : "Spring"}} ) db.Hours.updateMany( {"Season" : "2"}, { "$set" : { "Season" : "Summer"}} )``` – BoogieMan Sep 09 '21 at 05:53
  • not sure if you got the notification @joe – BoogieMan Sep 09 '21 at 06:09