0

Here is ruby code

 currency_conversion_rate = {
  "$project" => {
    'conversion_rate': '$conversion_rate',
  }
}

$conversion_rate is string type float number like 66.57. I have tried one method to use '$conversion_rate'.to_f but that makes it 0.0, other in mind is to somehow use parseFlaot, that I don't know yet how to use.

Other Details:

rails -> 5.0.2
mongoid -> 6.0.3
mongo -> 3.6.5
owsmmj
  • 168
  • 7
  • You didn't provide any line of code where `to_f` method is used. We need more information, how and where did you try to use it? Please, update your question with more details. – taras Jun 18 '18 at 14:11
  • Provided additional details thanks for constructive feedback. I am trying to use `parseDouble` somehow, I am going through manual maybe I get something – owsmmj Jun 18 '18 at 14:27
  • Doesn't that just mean that you're storing `conversion_rate` as a string? If you save your values as the correct type (float or decimal) this problem wouldn't be happening. See: https://stackoverflow.com/q/4973095/3982562 – 3limin4t0r Jun 19 '18 at 16:04
  • yeah, for now we migrated that value to new attribute where all values are stored as double – owsmmj Jun 20 '18 at 06:26

1 Answers1

0

Due to your question I only have one answer for you

You don't call the to_f on a string number like "66.57".to_f. You are calling .to_f on just a simple string "$conversion_rate".to_f which is simply 0.0. It is like "foobar".to_f

If you have $conversion_rate as variable you should use it correctly.

currency_conversion_rate = {
  "$project" => {
    'conversion_rate': $conversion_rate,
  }
}

or as alternative, although ugly code

currency_conversion_rate = {
  "$project" => {
    'conversion_rate': "#{$conversion_rate}",
  }
}
Denny Mueller
  • 3,505
  • 5
  • 36
  • 67
  • definitely your first point is right, I also checked it. But solution didn't worked `$conversion_rate` outputs `null` and `"#{$conversion_rate}"` outputs empty string. I think solution should be something that can process it on runtime like other operators `"$eq", "$multiply"` – owsmmj Jun 18 '18 at 15:42
  • https://jira.mongodb.org/browse/SERVER-11400 this issue talks about operators to convert. I doubt why it is closed when solution is not yet implemented. – owsmmj Jun 18 '18 at 15:44
  • Well then... Do you use `Modelname.collection.aggragate(currency_conversion_rate)`? – Denny Mueller Jun 18 '18 at 15:57
  • I am using `Modelname.collection.aggregate( {"$match"}, conversion_rate, ..)` in conversion rate is problem, it is string type and I need it in double. I don't know how `Modelname.collection.aggragate(currency_conversion_rate)` can help – owsmmj Jun 19 '18 at 05:40