We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to use conditions in ODM (MongoDB)

Hi,

I would like to retrieve records which has enddate 'lesser' than "today". Below is what i come out with, but does not work. Any advice appreciated.

$records = Records::find(array(
        '$lte' => array(
            array( 'endDateTime' =>  date())
        ),
        "limit" => 10,
        "sort"  => array("_id" => -1)
    ));


5.7k
Accepted
answer
edited Jul '15

I haven't used collections in Phalcon however you should be able to do something like this:

    $records = Records::find([
        [
            'endDateTime' => [
                '$lte' => new Mongodate(strtotime("now"))
            ],
        ]
        'limit' => 10,
        'sort'  => [
            "_id" => -1
        ]
    ]);


27.8k

Thanks alot! I managed to get it working too!

I haven't used collections in Phalcon however you should be able to do something like this:

  $records = Records::find([
      [
          'endDateTime' => [
              '$lte' => new Mongodate(strtotime("now"))
          ],
      ]
      'limit' => 10,
      'sort'  => [
          "_id" => -1
      ]
  ]);


27.8k

For benefit of others, here is my working code. I have also include a OR condition.

 $products = Products::find(array(
        array(
            '$or' => array(
                        array('endDateTime' => ''),
                        array('endDateTime' => array('$gt' => new Mongodate(strtotime("now"))))
            )               
        ),
        "sort"  => array("_id" => -1)
    ));