PHP with Mongo, questions with questions

3

I have a code very similar to the one in the photo, in the part that says '$gte' => 18 I just want to replace 18 with a variable that the user enters. I already have the variable and tried with '$gte' => $num but it does not work for me. What is the correct way to do this?

<?php
$personas = $bd->personas;

$personas->insert(array("nombre" => "Joe",   "edad" => 4));
$personas->insert(array("nombre" => "Sally", "edad" => 22));
$personas->insert(array("nombre" => "Dave",  "edad" => 22));
$personas->insert(array("nombre" => "Molly", "edad" => 87));

$edades = $bd->command(
    array(
        "distinct" => "personas",
        "key"      => "edad",
        "query"    => array("edad" => array('$gte' => 18))
    )
);

foreach($edades['values'] as $edad)
{
    echo "$edad\n";
}
?>

Image of the code: link

    
asked by Ivan Chaffardett 12.07.2016 в 20:56
source

1 answer

2

As indicated in the comments, it is necessary to make a cast of your variable, since mongo internally infers the type of data saved and based on that makes the queries.

your code would be:

$edades = $bd->command(
  array(
    "distinct" => "personas",
    "key"      => "edad",
    "query"    => array("edad" => array('$gte' => intval($num) ))
  )
);
    
answered by 13.07.2016 в 11:22