I've following collection in my MongoDB database
clat_output
{
"_id" : ObjectId("59fc6492421aa91e3e71d753"),
"output" : [
{
"time" : "100",
"clat" : "127"
},
{
"time" : "254",
"clat" : "294"
},
{
"time" : "354",
"clat" : "437"
}
...
...
]
}
I want to retrieve clat_output with a condition that time in clat_output should be in between a given range.
For example
I want to fetch clat_output having _id = ObjectId("59fc6492421aa91e3e71d753") with given time interval as 1 to 300
So it should give following output
{
"_id" : ObjectId("59fc6492421aa91e3e71d753"),
"output" : [
{
"time" : "100",
"clat" : "127"
},
{
"time" : "254",
"clat" : "294"
}
]
}
When I run the following query in mongodb console then it's giving me desired result.
db.clat_output.aggregate({
'$match' : {"_id" : ObjectId("59fc6492421aa91e3e71d753")}}, {
'$addFields' : {
'output' : {
'$filter' : {
'input' : '$output',
'as' : 'result',
'cond' : {
'$and' : [
{'$gte' : ['$$result.time', '1']},
{'$lte' : ['$$result.time', '300']}
]
}
}
}
}
});
But when I converted the same aggregate query into raw query in laravel, it's showing output as empty array. Following is my query in laravel
public function clat_output($id, $start, $end) {
$query = [
['$match' => ["_id" => $id]], [
'$addFields' => [
'output' => [
'$filter' => ['input' => '$output', 'as' => 'result',
'cond' => [
'$and' => [
['$gte' => ['$$result.time', $start]],
['$lte' => ['$$result.time', $end]]
]
]
]
]
]
]
];
$result = self::raw(function ($collection) {
return $collection->aggregate($query);
});
return $result;
}
'time'as a "string" and as such a value like'5'does not fall "between" the values of'1'and'300'since it begins with'5'and as a "string" that is not within the range. You actually want "numeric" values if you want to query for "numeric ranges". So you really should convert the data.