Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

php - How does sorting work in the new mongodb PECL extension?

I have just shifted from the old Mongo extension to the newest PHP driver (mongodb). I'm also using the PHP library provided for mongodb.

Previously, I could simply do ->sort() on a cursor instance, but that doesn't seem to be the case any more since I get this error:

Fatal error: Uncaught Error: Call to undefined method MongoDBDriverCursor::sort()

What is the alternative to sorting/limiting/skipping now?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got an answer about this on the respective Github repository.

To be able to sort with find, you simply use the second find parameter like this:

$filter  = [];
$options = ['sort' => ['username' => 1]];

$client = new MongoDBClient('mongodb://localhost');
$client->mydb->mycollection->find($filter, $options);

For more information, look into the issue I have posted at the repository:

https://github.com/mongodb/mongo-php-driver/issues/214


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...