MongoDB queries in Lithium - should I use `order` or `$orderby`, or doesn't it matter?

StackOverflow https://stackoverflow.com/questions/10972477

  •  13-06-2021
  •  | 
  •  

Pergunta

The following two criteria sets give me identical results using Lithium and MongoDB. Both are equally easy to read and write. Should I prefer one over the other for efficiency reasons, or is one just Lithium/MongoDB syntactic sugar for the other?

$criteria = array(
    'fields' => array('_id', 'title', 'created', 'edited', 'username'),
    'order' => {'edited' => 'ASC'},
    'limit' => 3
);

And

$criteria = array(
    '$orderby' => array('edited' => 'ASC'),
    'fields' => array('_id', 'title', 'created', 'edited', 'username'),
    'limit' => 3
);

Follow up question: I really struggled to figure out the right syntax to use for order and $orderby, and in general I find Lithium's code pretty hard to grok. Do you have any suggestions on how best to approach the codebase for better/faster understanding?

PS: I realise the follow up question might not really be StackOverflow style - but there are other posts on SO and elsewhere which hint at exactly this problem (e.g. Lithium apps that go beyond CRUD). Any input on this could be really valuable!

Foi útil?

Solução

Lithium translate 'order' to a MongoBD sort() source

MongoDB's $orderby directive is equivalent to calling sort() after a find() source

So both are equivalent.
I'll go with order since it's a consistent and unified API to interact with any underlying datasource.

Hope it helps

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top