Mongo - Fetch first created and last created document according to 'ts' (timestamp)
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);
精彩评论