If my doc contains normal key-val pairs, the convert $toDouble/$toInt and then comparison works fine. But my doc looks like this:
{ "_id" : "14_0", "data" : [ { "dn" : "subdata=data_a", "userUL" : "0", "objectClass" : "NEWDATA", "userDL" : "5" } ] }
{ "_id" : "15_0", "data" : [ { "dn" : "subdata=data_b", "userUL" : "0", "objectClass" : "NEWDATA", "userDL" : "3" } ] }
{ "_id" : "16_0", "data" : [ { "dn" : "subdata=data_c", "userUL" : "0", "objectClass" : "NEWDATA", "userDL" : "9" } ] }
For an array it throws conversion error given below.
db.testcol.find({ $expr: { $lte: [ { $toDouble: "$data.userDL" }, 5 ] } }).count()
2020-09-18T06:26:37.010+0530 E QUERY [js] uncaught exception: Error: count failed: {
"ok" : 0,
"errmsg" : "Unsupported conversion from array to double in $convert with no onError value",
"code" : 241,
"codeName" : "ConversionFailure"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBQuery.prototype.count@src/mongo/shell/query.js:376:11
@(shell):1:1
So I tried with aggregate:
> db.testcol.aggregate([{ $project:{ adjustedGrades:{$map:{input: "$data.userDL",as: "grade",in: {$lte : [{ $toInt: "$grade" },5] } }}}}, {$match: {adjustedGrades: {$eq: true}}}])
{ "_id" : "14_0", "adjustedGrades" : [ true ] }
{ "_id" : "15_0", "adjustedGrades" : [ true ] }
I want to be able to convert my string to Int/Double and do the comparison inside find() and not aggregate(). I mean if the convert and compare is working for normal key-value pair using find() then, mongoDB should also be able to support for array data also right? Array data is a commonly given in docs and querying it should be as easy as — db.testcol.find({ $expr: { $lte: [ { $toDouble: “$data.userDL” }, 5 ] } }).count()