I have imported a huge database from csv to mongodb.
It sadly imported every value as strings.
I have more then 1000 fields and 1M documents.
how can i change the type from ALL fieldvalues in ALL documents from string to int without doing it for every field manually?
Thanks for help.
2
Answers
I think you cannot change the datatype of all the fields in one go. You will have to do it field by field.
Well, if you don’t have nested documents, then this will work for you:
In this, we first convert the document into an array, using
$objectToArray
.Then, we loop over the array and convert the values to
int
. Finally, we construct a new document from the array using$arrayToObject
, and then we merge these new documents into the collection using$merge
.Here’s the playground link.