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 deal with embedded document of mongodb ?

I have an array in an embedded document and I want to sort this array by one of it's element, can I implement this in a way like the ODM in phalcon? for example, can I sort it in this way?

find(array(
    'conditions' => array(
        'time' => array($compare => $time)),
    'sort' => array('time' => -1),
    'limit' => $count
));

image below is my document in mongodb, and I want to sort the elements of array called document by $time, can phalcon implement this? I know mongodb itself can, but I don't know how to do this in phalcon. Thanks!



98.9k
Accepted
answer

Yes, it must work this way:

MyModel::find(array(
    'conditions' => array(
        'time' => array($compare => $time)),
    'sort' => array('time' => -1),
    'limit' => $count
));

https://docs.phalcon.io/en/latest/reference/odm.html#finding-documents



2.1k

thank you for helping , and what I want is : sort the array in the red circle by time, and this array is in an inner document, so I think this way in phalcon's document can't work. anyway, I can't get my work done. So do you have any other suggestion or where I was wrong?



98.9k

Try:

MyModel::find(array(
    'conditions' => array(
        'time' => array($compare => $time)),
    'sort' => array('bookmark.document.time' => -1),
    'limit' => $count
));