I am fairly new with MongoDB, so apologizes for what might be a basic question.
I have a bunch of documents in a collection (call it CollA
), and taking a subset of it and the particular field in question would look like:
docs = [
{Value: "1,000,000+"},
{Value: "40,000"},
{Value: "2,000,000+"},
{Value: 500000},
{Value: 400000}]
Note the mixed float and string datatypes within the field "Value".
I am trying to find a way to:
- Convert "Value" to a float (so I assume remove the "," and do a
$ToDouble
on it) - In the cases where the string contains a "+", I would like to set a new field "Truncated" to "true".
The desired output would be
docs = [
{Value: 1,000,000,
Truncated: true},
{Value: 40,000},
{Value: 2,000,000,
Truncated: true}},
{Value: 500000},
{Value: 400000}]
So now they would all be "doubles", and the new field would be there in the documents where the "+" existed.
Thanks so much!
2
Answers
You can use $type to differentiate strings vs numbers.
For string it’s a combinations of if-else conditions, to add "Truncated" field, remove non-number characters, and eventually convert to number with $toDouble.
The final $replaceWith is to bring additional fields to the root level:
Query
Truncated
Playmongo