Equivalent of charindex SQL Server in MongoDB and sorted field by searched word position.

225 views
Skip to first unread message

marcandre

unread,
Sep 3, 2012, 10:57:35 AM9/3/12
to mongod...@googlegroups.com
Hellow everybody,
 
I am using MongoDB database with php and I would like to know how can we sort a list with a field sorted by searched word position in a text ?
 
But no field sorted by alphabetic order . I give you the SQL code in MS SQL Server : "select * from my_table order by charindex( <word what I m looking for> ,my_field_to_sort) ",
I would like what is the equivalent in MongoDB of this SQL code ? Charindex give us string position in the text field .
 
Thank you very much .
 
Best regards .

Stephen Steneker

unread,
Sep 3, 2012, 5:00:30 PM9/3/12
to mongod...@googlegroups.com
I am using MongoDB database with php and I would like to know how can we sort a list with a field sorted by searched word position in a text ?
 
But no field sorted by alphabetic order . I give you the SQL code in MS SQL Server : "select * from my_table order by charindex( <word what I m looking for> ,my_field_to_sort) ",
I would like what is the equivalent in MongoDB of this SQL code ? Charindex give us string position in the text field .

Hi,

You have to provide one or more key fields to MongoDB sort() .. you can't use functions.

Your equivalent implementation would have be done with a client-side sort in your app.

For example, using usort() in PHP:
 http://php.net/manual/en/function.usort.php

Cheers,
Stephen

craiggwilson

unread,
Sep 3, 2012, 5:51:45 PM9/3/12
to mongod...@googlegroups.com
Or, rather that client-side, you could also denormalize and store the indexes of the words you are looking for at a higher level.  This would be a schema change, but would result in much faster queries.

marcandre

unread,
Sep 4, 2012, 11:10:14 AM9/4/12
to mongod...@googlegroups.com
Good afternoon .
 
For the client-side in php I would like to know how can we convert MongoDB cursor to array before sort this array in php and read several fields in array.
 
I am afraid to increase execution time if I make a php loop to get an array from Mongo Cursor .
 
Whitch php function what convert cursor to array without make a loop in php .
 
Thank you for your response .
 
Best regards .

Sam Millman

unread,
Sep 4, 2012, 11:24:25 AM9/4/12
to mongod...@googlegroups.com
"For the client-side in php I would like to know how can we convert MongoDB cursor to array before sort this array in php and read several fields in array."

Iterator to array: http://php.net/manual/en/function.iterator-to-array.php.

There is no way to do it without a loop it is merely a question of how the loop is executed whether witin the bowls of PHP or within your App code.

--
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
See also the IRC channel -- freenode.net#mongodb

Sam Millman

unread,
Sep 4, 2012, 11:25:53 AM9/4/12
to mongod...@googlegroups.com
As a note a loop can run approx about 50K times a second I believe in PHP (or something like that when I tested it on 5.1) and it uses the same piece of memory each time so a loop isn't too shabby.
Reply all
Reply to author
Forward
0 new messages