Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo get users whose id ends in a certain digit

In mongo, I'm trying to query for users with an id that ends in a certain number. This number is a parameter that I pass in.

So if I had 4 users [3544,42345,66452,7348], how could i query and just get user 7348 if i was just interested in users whose id ends in 8.

I've looked through here, but can't find exactly what would work for this:

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions

UPDATE: doing this is PHP, and the expected values are int's, not strings. My where clause looks like this: $where = array('uid'=>'/8$/');

like image 481
user1082428 Avatar asked Mar 02 '12 22:03

user1082428


1 Answers

If you want to find a user whose id end in the digit 8 you would just have to do this:

db.users.find( { id : /[8]$/ } );

$ matches to the end of the line. just like ^ matches the beginning of the line. refer to this for further details

For finding last digit of int type data fields you would have to use the $mod operator.

like image 165
nightf0x Avatar answered Oct 01 '22 01:10

nightf0x