How do i change data of type String to Int in MongoDB. ( Preferably in MongoCompass or Terminal )
I have received a pretty huge dataset with the documents in the following format
To add it to my existing data i need to change the ean from type string to type int. I already tried the following:
db.products.aggregate([{$project: {ean: { $toInt: "$ean"}}}])
Which gave me the error:
MongoError: Failed to parse number '0002983542515' in $convert with no onError value: Overflow
So i tried the same with convert:
db.products.aggregate([{$project: {_id: 0, result:{ $convert: { input: "ean", to: "int", onError: "An error occured", onNull: "input was null or empty"}}}}])
This is the console.log i got from that:
[ { result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' },
{ result: 'An error occured' } ]
My ean field didn't change, its still from type String.
Any help is highly appreciated
Trying Rakhi Agrawal solution:
but its still a string: