I am currently trying to compare two strings which are numbers in a find query. I need to use strings because the numbers would cause an overflow in Javascript if I save them as such. The to compared value comes via an API call and the value is inside an array:
const auction = await this.findOne({
$and: [{
$or: [
{ 'bids.amount': amount },
{ 'bids.signature': signature },
{ 'bids.amount': { $gte: amount } }
]
}, { 'tokenId': tokenId }, { 'isActive': true }]
});
How would I change the query in order to handle the strings as numbers, so my comparison would actually be correct?
CodePudding user response:
The bellow query assumes that bids is an array like
bids=[{"amount" : "1224245" , "signature" : "234454523"} ...]
If signature is not a number remove the $toLong
from signature.
aggregate(
[{"$match":
{"$expr":
{"$and":
[{"$eq": ["$tokenId", "tokenId_VAR"]},
{"$eq": ["$isActive", true]},
{"$reduce":
{"input": "$bids",
"initialValue": false,
"in":
{"$or":
["$$value",
{"$gte": [{"$toLong": "$$this.amount"}, "amount_VAR"]},
{"$eq": [{"$toLong": "$$this.signature"}, "signature_VAR"]}]}}}]}}}])