Update or Retrieve from peculiar field name in MongoDB Atlas database

Viewed 67

I am stuck in a unique problem. In my database of MongoDB Atlas, I used java to upload documents. Unknowingly, a field name was set as "Vehicle Type", with space in between. This is how it looks in database

enter image description here

Since database is huge, what is the best way to rectify the field name for all documents ?

Also if it is possible, how can I retrieve the data from that field in NodeJS using mongoose? Currently in Mongosh (CLI), the field "Vehicle Type" shows as 'Vehicle Type', which I am unable to use :

enter image description here

1 Answers

if you can connect via CLI then just do this to update all records:

db.collection.update({},{$rename: { "Vehicle Type": "VehicleType" }},{multi:true})

Please change the collection to your collection in above command and also above will change field name "Vehicle Type" to "VehicleType"

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

Related