I am trying to code up something using an SDK that uses mongodb behind the scenes. The backend has a collection, that stores number values as a string.
Now I want query that collection and check if the value in that string column is greaterThan or lessThan a given value.
Since the column is of type string, I dont know how I can write an aggregation that parses this string value to a number value.
Example
collection { balance: string }
My goal, query collection WHERE balance >= someValue
CodePudding user response:
I believe you are looking for somehting like this:
model.aggregate([
{
'$addFields': {
'decimalBalance': {
'$toDecimal': '$balance'
}
}
}, {
'$match': {
'decimalBalance': {
'$gte': value
}
}
}
])
You can $unset or not $project the extra Field, I don't know how to do it without the extra field.
For reference look here: