开发者

Sort Value in Mongodb

开发者 https://www.devze.com 2023-02-15 09:11 出处:网络
I\'m writing PHP application with Mongodb. I can search like $regex=\'/.*sim.*/i\'; $find[\'Word\']=new mongoRegex($regex);

I'm writing PHP application with Mongodb. I can search like

$regex='/.*sim.*/i'; 
$find['Word']=new mongoRegex($regex); 
$cursor = $collection->find($find); 

and got result like this

开发者_如何转开发

assimilate

facsimile

fortissimo

generalissimo

passim

persimmon

persimmons

similar

similarly

simple

simple hurt

simpler

simplest

simply

But I want to sort like

1. sim 
2. sim* 
3. *sim* 
4. sim* 

How to wrote regular expression or how to sort like this ? In MySQL, I wrote

SELECT * , IF( `Word` = 'sim', 1, IF( `Word` LIKE 'sim%', 2, IF( `Word` LIKE '%sim', 4, 3 ) ) ) AS `sort` FROM `dblist` WHERE `Word` LIKE '%sim%' ORDER BY `sort` , `Word`

Now, I'm planning to change MySQL to Mongodb but problem is searching result. I can't sort result like MySQL in mongodb.

Update:

I can sort after fetch the data but the problem is paging. The result will come over 1000 per times. I don't want to fetch all result from database. So, sort in database and fetch the result with paging.


I'm pretty sure you'll have to do that kind of sorting in your own code after fetching the result. I'm not aware of any equivalent to SQL's AS.


Mongodb collection has sort operation, if you want more complex operations, do it on client side.

0

精彩评论

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