2

I've a problem that's getting me crazy!

I need to translate the following Mongo query in PHP:

db.mycollection.find({$or: [ {'field.a': 45.4689, 'field.b': 9.18103}, {'field.a' : 40.71455, 'field.b': -74.007124} ]})

It works perfectly from the shell.

I think the query should be translated to PHP in the following way (var_dump):

Array
    (
        [$or] => Array
            (
                [0] => Array
                    (
                        [field.a] => 45.468945
                        [field.b] => 9.18103
                    )

                [1] => Array
                    (
                        [field.a] => 40.71455
                        [field.b] => -74.007124
                    )

            )

    )

but I get no results in PHP!

Why? What's wrong? What's the correct syntax?

Thank you!

1
  • How can you say it does not work with PHP? Probably you have written some PHP code that does not create the exact same query but something differently? Please provide proof you make that query in PHP, e.g. by sharing your PHP code and sharing the generated query string. - The query you share is an array and not a string like within the shell. Commented Apr 30, 2012 at 9:28

1 Answer 1

3

Your syntax seems fine to me, I think the main problem is is that you are using floating point numbers which are not always as accurate as you think—especially if you mix up 45.4689 and 45.468945 yourself. For direct comparing of floating point numbers, you should always add a small fuzzing factor.

In this case you seem to be using coordinates? If that's the case, I suggest you:

  • swap the a and b fields (so that you get longitude, then latitude)
  • create a 2d index on "field": ensureIndex( array( 'field' => '2d' ) );
  • use geoNear with a small max distance and a limit of 1,

That should give a much better way of scanning for points. You'd have to run the query twice though, as the geoNear command can't do $or.

If you only have a discrete set of points (like your comments seems to indicate), then I would recommend to not query on floating point numbers but simply add a field naming the city as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Derick, you're right! Yes, they're coordinates and I was trying to do what you guessed. You were right, the problem lies in accuracy... The fact is that the query should work like this: "Get the first 10 results that. eg., are located in New York or Milan ordered by another field" and I mean 10 totally, NYC+Milan (so the 1st result could be NYC, the 2nd Milan, 3rd, 4th NYC again and so on). The total number of documents is huge, I just need 10 combined. So I can't use spatial queries, neither I can aggregate results on application level. So, it's just impossible to do? :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.