2

i'm new to mongoDB. I got my data as a string but i want to parse it to a decimal/ float. I found made this code but it doesn't seem to work. This is my code, it replaces - for 00, * for "", and parses it a float. It gives no errors, but the parseFloat(doc).toFixed(2).

db.behuizingen.find().forEach(function(doc)
{
    var price = doc.Prijs.replace('€', ''); // it may be vary for your other document
    price = price.replace('-', '00');  
    price = price.replace('*', '');  
    doc.Prijs = Number(price);
    parseFloat(doc).toFixed(2)

    db.behuizingen.update({_id : doc._id} , doc;
})

Thanks in advance.

2 Answers 2

4

You did this wrong. Convert first and the Number() function of the shell has nothing to do with it. So replacing that line an continuing:

doc.Prijs = parseFloat(parseFloat(price).toFixed(2));
db.moederborden.update({_id : doc._id} , doc );

But also be beware. That usage of .update() where the second argument is there will "replace" the entire document with the contents of doc.

You may want to do this instead:

doc.Prijs = parseFloat(parseFloat(price).toFixed(2));
var update = { "$set": {} };

Object.keys(doc).each(function(key) {
    update["$set"][key] = doc[key];
});

db.moederborden.update({_id : doc._id} , update );

Which uses the $set operator, so that any existing values that were not referenced in your doc object are not overwritten when writing to the database.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. Now it changes the ,into a .. The type still says it is a string. How do I fix this?
@henktenk Sorry, I typed that wrong didn't I. It was meant to be wrapped twice. parseFloat() emits numeric from string, .toFixed() emits a string. So wrap all of that in parseFloat() again as shown.
0

I had a similar issue where we had not had decimal serialisation enabled correctly and so I wanted to update documents that were already in our database to use the Decimal type instead of String. I used the following script:

db.MyCollection.find({ Price: { $type: "string" } }).forEach(function(doc) { 
    print('Updating price for ' + doc._id);
    db.MyCollection.update( 
       { _id: doc._id },
       { $set : { Price: NumberDecimal(doc.Price) } }
    )
})

This only retrieves the documents that need updating by filtering on the field type, and then uses NumberDecimal to convert the string into a decimal.

2 Comments

NumberDecimal was introduced in MongoDB v4.0. I am using v3.2 and using parseFloat and parseInt seems to be the only way to change the data type of a field.
@Sheng I think it was actually introduced in v3.4 (docs.mongodb.com/manual/tutorial/model-monetary-data/…), so still later than your version but earlier than v4.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.