Strange behaviour of queries when space in fields

97 views
Skip to first unread message

newtomongo

unread,
Jan 24, 2012, 11:08:17 AM1/24/12
to mongodb-user
I am very new to Mongo, but have a system up and running and done some
queries. However, if I do a 'find' when the field contains spaces e.g
[Regular ID] not [Regular_ID], it does not work! (part of a record):

[_id] => MongoId Object (
[$id] => 000000000000000000000000
)
[Regular ID] => 5176
[External ID] => MAL_146
......
.....

$query = array( "Regular ID" => array( '$lt' => "1000" ) );
$cursor = $collection->find( $query );

foreach ($cursor as $obj) {
echo $obj["Regular ID"] . chr(10);
}
?>

I just get nothing although 'Regular IDs' exist below 1000.
If I change the record field to [Regular_ID] it works... I just can't
find any places where it is explained why this should not work ?? Is
there a way to handle spaces in fields so queries will work as
expected ?

If I search directly on the ID (not using $lt / $gt), it also works

Thanks!

Ben Becker

unread,
Jan 24, 2012, 2:50:09 PM1/24/12
to mongod...@googlegroups.com
Hi,

This query has a minor issue: array( '$lt' => "1000" ) is comparing the string "1000" instead of the number 1000.  While that query is valid, it may not produce the results expected (as it does alphabetical ordering instead of numerical, and the data must be inserted as a string too).  Can you try using array('$lt' => 1000) to see if you get the expected results?  Aside from that, spaces should work as expected.  Some more details on the field types can be found here: http://www.mongodb.org/display/DOCS/Data+Types+and+Conventions.

It does seem a bit odd that the object you pasted is '[$id] => 000000000000000000000000' though; what version of mongo and php-mongo are you running?  
Reply all
Reply to author
Forward
0 new messages