0

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

enter image description here

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:

enter image description here

but its still a string:

enter image description here

Darjusch
  • 188
  • 13
  • 1
    try `$toLong` operator.. because integer exceeded the limit. – turivishal May 06 '21 at 12:27
  • Good point unfortunately that did not solve the issue – Darjusch May 06 '21 at 13:55
  • db.products.aggregate([{$project: {ean: { $toLong: "$ean"}}}]) [ { _id: ObjectId("60522fe2eec53799e3faa6bf"), ean: null }, { _id: ObjectId("605a77cb5177059bebf0ad0a"), ean: null }, ... ] – Darjusch May 06 '21 at 14:00
  • what are you getting in result? see working [playground](https://mongoplayground.net/p/dTan13ucrjC) – turivishal May 06 '21 at 14:01
  • It is working in the playground but not on the MongoDB compass. Maybe because the field was created as string? – Darjusch May 06 '21 at 14:13
  • no, make sure any of the document in `ean` field should not contain any alphabets / special characters. – turivishal May 06 '21 at 14:16
  • what are you actually want to do? do you want to update that field from string to number in database permanently? it just want temporary formatted result? – turivishal May 06 '21 at 14:19
  • I have to collections right now. One with my production data and one with this new data, now i want to add the new data to my production data but the data types have to be the same. The only data type that is different is the ean. – Darjusch May 06 '21 at 14:27
  • okay where you are executing this converter `$toLong` query? – turivishal May 06 '21 at 14:43

1 Answers1

0

Are you looking for something like this SO post?

You may also find many examples over internet for the same. Below is the snippet from tutorialspoint

 db.demo369.insertOne({"Price":"1000000"});
 db.demo369.find().forEach( function (d) {
     d.Price= parseInt(d.Price);
     db.demo369.save(d);
 });

Hope I got the question right.

Rakhi Agrawal
  • 827
  • 7
  • 14
  • Thank you for your suggestion i tried that and it did not change the type of the field it is still string – Darjusch May 06 '21 at 13:47