Reputation: 24967
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.
Upvotes: 0
Views: 634
Reputation: 2334
Mongodb collection has sort operation, if you want more complex operations, do it on client side.
Upvotes: 0
Reputation: 329
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
.
Upvotes: 1