MySQL order by in mongodb findOne()
In my mongo collection, I have several records with timestamps. I want to use findOne()
and return the oldest record with a where parameter.
If it's not possible to use findOne()
, it's alright. I jus开发者_Go百科t need to return the oldest record with a where parameter.
How can this be done in MongoDB?
If you need an oldest record, use
db.collection.find().sort({ created: *1* }).limit(1)
I had to do this.
$request = $collection_requests->find( array( 'status' => 0 ) );
$request->sort( array( 'created' => 1 ) );
$request->limit(1);
$request->next();
$request = $request->current();
You can try this:
db.collection.find().sort({timestamps : -1}).limit(1);
精彩评论