FindOne With Sort | Php

2,524 views
Skip to first unread message

Nothing

unread,
Feb 25, 2011, 10:27:11 AM2/25/11
to mongodb-user
Hi, i've a question.
I must get a single record from mongodb with "findOne" function.

This Work:

-- $collection->findOne(array('enabled' => 1,'last_index' =>
array('$lt' => (int)$update)));

But i want the "sort" function, but this not work:

-- $collection->findOne(array('enabled' => 1,'last_index' =>
array('$lt' => (int)$update))).sort({last_index:1});

Why?

Tim Hawkins

unread,
Feb 25, 2011, 10:36:39 AM2/25/11
to mongod...@googlegroups.com
>> $collection->find(array('enabled' => 1,'last_index' =>array('$lt' => (int)$update)))->sort(array('last_index'=>1))->limit(1)->current();


Find one returns an array containing the document, not a cursor, you need a cursor to sort

->current() will convert the top item on the result to an array;

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>

Nothing

unread,
Feb 25, 2011, 11:12:45 AM2/25/11
to mongodb-user
Umh. No :(
current() function not work!

Return is empty.

Nothing

unread,
Feb 25, 2011, 11:48:53 AM2/25/11
to mongodb-user
Ok so...i found this method:

>> $obj = $collections->find(array('enabled' => 'Y','last_index' => array('$lt' => (int)$update)))->sort(array('last_index' => 1))->limit(1);
>> $var = $obj->getNext();
>> print_r($var);

This work fine, BUT i must use the cursor.
Where can i found a function for get a first row?

Jonathan Wage

unread,
Feb 25, 2011, 11:52:32 AM2/25/11
to mongod...@googlegroups.com, Nothing
I think this is the only way to do it.

- Jon
--
Connect with me on http://twitter.com/jwage and http://about.me/jwage to keep in touch.

Nothing

unread,
Feb 25, 2011, 11:54:26 AM2/25/11
to mongodb-user
Ok. Thank you.
> Connect with me on *http://twitter.com/jwage*<http://twitter.com/jwage>
>  andhttp://about.me/jwageto keep in touch.
Reply all
Reply to author
Forward
0 new messages