开发者

Mongo - Fetch first created and last created document according to 'ts' (timestamp)

开发者 https://www.devze.com 2023-02-14 08:16 出处:网络
Is it possible to fetch first created and last created document according to \'ts\' (timestamp) element of the doc开发者_JAVA技巧ument in single mongo query?

Is it possible to fetch first created and last created document according to 'ts' (timestamp) element of the doc开发者_JAVA技巧ument in single mongo query? If yes then please let me know how to query the mongo using php.


You can't get them in one query, but you can do it in 2 queries.

db.collection.find().limit(1).sort([ts:-1])  --> Find the last timestampe
db.collection.find().limit(1).sort([ts:1])   --> Find the first timestamp 

or in PHP

$cursor = $collection->find()->sort(array('ts'=>-1))->limit(1);
$cursor = $collection=>find()->sort(array('ts'=>1))->limit(1);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号