[SERVER-17483] $or with geoIntersect fail Created: 05/Mar/15  Updated: 06/Mar/15  Resolved: 06/Mar/15

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 2.6.8
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Murillo Freitas Assignee: David Storch
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

When i execute this query

 
db.regions.find({
    $or: [{
        location: {
          $geoIntersects: {
             $geometry: [{
                type: "Polygon",
                coordinates: [ [ [-54.008789,-29.592565],
                                    [-53.547363,-29.420460],
                                    [-53.459473,-29.878755],
                                    [-53.964844,-29.821583],
                                    [-54.008789,-29.592565] ] ]
             }
             ]
          }
       },
       location: {
          $geoIntersects: {
             $geometry: {
                type: "Polygon",
                coordinates: [ [ [ -47.705147, -21.167139 ],
                                [ -47.795612, -21.090562 ],
                                [ -47.909596, -21.167139 ],
                                [ -47.808744, -21.245238 ],
                                [ -47.705147, -21.167139 ]  ] ]
             }
          }
       }
   }]
})

just the last condition is returned



 Comments   
Comment by David Storch [ 06/Mar/15 ]

Glad to hear it! Closing this ticket as Works as Designed.

Comment by Murillo Freitas [ 06/Mar/15 ]

Hi,

Yes, it is. Worked perfectly!

many thanks

Comment by David Storch [ 06/Mar/15 ]

Hi mmbfreitas,

After reviewing your reproduction steps, I believe that the system is working as designed. As originally given in the description of the ticket, the query has the following structure:

db.regions.find({
    $or: [
        {location: <geoPredicate1>, location: <geoPredicate2>}
    ]
})

Since the $or has just one clause, this is logically equivalent to an $and of the two geo predicates:

db.regions.find({location: <geoPredicate1>, location: <geoPredicate2>});

However, JavaScript objects do not permit duplicate field names. Since both geo predicates are against the field location, one overwrites the other. Only one of the $geoWithin predicates actually gets delivered to the server, which explains the behavior you are seeing.

I think this derives from a mistake in the query. I have updated your query so that it is an $or of the geo predicates, which may be what you intended. Could you try the query below and confirm that this works as expected?

db.regions.find({
    $or: [{
        location: {
          $geoIntersects: {
             $geometry: {
                type: "Polygon",
                coordinates: [ [ [-54.008789,-29.592565],
                                    [-53.547363,-29.420460],
                                    [-53.459473,-29.878755],
                                    [-53.964844,-29.821583],
                                    [-54.008789,-29.592565] ] ]
             }
          }
       }},
       {
       location: {
          $geoIntersects: {
             $geometry: {
                type: "Polygon",
                coordinates: [ [ [ -47.705147, -21.167139 ],
                                [ -47.795612, -21.090562 ],
                                [ -47.909596, -21.167139 ],
                                [ -47.808744, -21.245238 ],
                                [ -47.705147, -21.167139 ]  ] ]
             }
          }
       }
   }]
})

Best,
Dave

Comment by Murillo Freitas [ 06/Mar/15 ]

Could you reproduce the issue??

Comment by Murillo Freitas [ 05/Mar/15 ]

and the explain result is

/* 0 */
{
    "cursor" : "BtreeCursor location_2dsphere",
    "isMultiKey" : true,
    "n" : 1,
    "nscannedObjects" : 1,
    "nscanned" : 1,
    "nscannedObjectsAllPlans" : 1,
    "nscannedAllPlans" : 1,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 1,
    "indexBounds" : {
        "location" : [ 
            [ 
                "4f2211303", 
                "4f2211303"
            ], 
            [ 
                "4f22113030", 
                "4f22113030"
            ], 
            [ 
                "4f221130302", 
                "4f221130302"
            ], 
            [ 
                "4f2211303022", 
                "4f2211303023"
            ], 
            [ 
                "4f2211303023", 
                "4f2211303024"
            ], 
            [ 
                "4f22113031", 
                "4f22113031"
            ], 
            [ 
                "4f221130313", 
                "4f221130313"
            ], 
            [ 
                "4f2211303130", 
                "4f2211303131"
            ], 
            [ 
                "4f2211303132", 
                "4f2211303133"
            ], 
            [ 
                "4f2211303133", 
                "4f2211303134"
            ], 
            [ 
                "4f22113032", 
                "4f22113032"
            ], 
            [ 
                "4f221130320", 
                "4f221130320"
            ], 
            [ 
                "4f2211303200", 
                "4f2211303201"
            ], 
            [ 
                "4f2211303203", 
                "4f2211303203"
            ], 
            [ 
                "4f22113032032", 
                "4f22113032033"
            ], 
            [ 
                "4f22113033", 
                "4f22113033"
            ], 
            [ 
                "4f221130331", 
                "4f221130331"
            ], 
            [ 
                "4f2211303311", 
                "4f2211303311"
            ], 
            [ 
                "4f22113033111", 
                "4f22113033112"
            ]
        ]
    },
    "server" : "Tiba-PC:27017",
    "filterSet" : false,
    "stats" : {
        "type" : "KEEP_MUTATIONS",
        "works" : 2,
        "yields" : 0,
        "unyields" : 0,
        "invalidates" : 0,
        "advanced" : 1,
        "needTime" : 0,
        "needFetch" : 0,
        "isEOF" : 1,
        "children" : [ 
            {
                "type" : "FETCH",
                "works" : 2,
                "yields" : 0,
                "unyields" : 0,
                "invalidates" : 0,
                "advanced" : 1,
                "needTime" : 0,
                "needFetch" : 0,
                "isEOF" : 1,
                "alreadyHasObj" : 0,
                "forcedFetches" : 0,
                "matchTested" : 1,
                "children" : [ 
                    {
                        "type" : "IXSCAN",
                        "works" : 2,
                        "yields" : 0,
                        "unyields" : 0,
                        "invalidates" : 0,
                        "advanced" : 1,
                        "needTime" : 0,
                        "needFetch" : 0,
                        "isEOF" : 1,
                        "keyPattern" : "{ location: \"2dsphere\" }",
                        "isMultiKey" : 1,
                        "boundsVerbose" : "field #0['location']: [\"4f2211303\", \"4f2211303\"], [\"4f22113030\", \"4f22113030\"], [\"4f221130302\", \"4f221130302\"], [\"4f2211303022\", \"4f2211303023\"), [\"4f2211303023\", \"4f2211303024\"), [\"4f22113031\", \"4f22113031\"], [\"4f221130313\", \"4f221130313\"], [\"4f2211303130\", \"4f2211303131\"), [\"4f2211303132\", \"4f2211303133\"), [\"4f2211303133\", \"4f2211303134\"), [\"4f22113032\", \"4f22113032\"], [\"4f221130320\", \"4f221130320\"], [\"4f2211303200\", \"4f2211303201\"), [\"4f2211303203\", \"4f2211303203\"], [\"4f22113032032\", \"4f22113032033\"), [\"4f22113033\", \"4f22113033\"], [\"4f221130331\", \"4f221130331\"], [\"4f2211303311\", \"4f2211303311\"], [\"4f22113033111\", \"4f22113033112\")",
                        "yieldMovedCursor" : 0,
                        "dupsTested" : 1,
                        "dupsDropped" : 0,
                        "seenInvalidated" : 0,
                        "matchTested" : 0,
                        "keysExamined" : 1,
                        "children" : []
                    }
                ]
            }
        ]
    }
}

Comment by Murillo Freitas [ 05/Mar/15 ]

Hi, sure.

in my collection have this 3 documents bellow, when I execute that query, only the last condition of query returns to me

/* 0 */
{
    "_id" : 1,
    "name" : "Sao Paulo",
    "prefix" : "SP",
    "location" : {
        "type" : "Polygon",
        "coordinates" : [ 
            [ 
                [ 
                    -53.052979, 
                    -22.593726
                ], 
                [ 
                    -50.976563, 
                    -19.911384
                ], 
                [ 
                    -47.438965, 
                    -19.973349
                ], 
                [ 
                    -46.362305, 
                    -22.847071
                ], 
                [ 
                    -44.813232, 
                    -22.390714
                ], 
                [ 
                    -44.165039, 
                    -22.69512
                ], 
                [ 
                    -44.769287, 
                    -23.362429
                ], 
                [ 
                    -48.14209, 
                    -25.22482
                ], 
                [ 
                    -49.306641, 
                    -24.726875
                ], 
                [ 
                    -49.987793, 
                    -22.998852
                ], 
                [ 
                    -53.052979, 
                    -22.593726
                ]
            ]
        ]
    }
}
 
/* 1 */
{
    "_id" : 2,
    "name" : "Parana",
    "prefix" : "PR",
    "location" : {
        "type" : "Polygon",
        "coordinates" : [ 
            [ 
                [ 
                    -53.063965, 
                    -22.573438
                ], 
                [ 
                    -49.790039, 
                    -23.079732
                ], 
                [ 
                    -48.098145, 
                    -25.22482
                ], 
                [ 
                    -48.757324, 
                    -28.555576
                ], 
                [ 
                    -49.899902, 
                    -29.573457
                ], 
                [ 
                    -53.76709, 
                    -27.098254
                ], 
                [ 
                    -54.602051, 
                    -25.562265
                ], 
                [ 
                    -54.492188, 
                    -23.926013
                ], 
                [ 
                    -53.063965, 
                    -22.573438
                ]
            ]
        ]
    }
}
 
/* 2 */
{
    "_id" : 3,
    "name" : "Rio Grande do Sul",
    "prefix" : "RS",
    "location" : {
        "type" : "Polygon",
        "coordinates" : [ 
            [ 
                [ 
                    -53.4375, 
                    -33.742613
                ], 
                [ 
                    -57.65625, 
                    -30.278044
                ], 
                [ 
                    -53.679199, 
                    -26.509905
                ], 
                [ 
                    -49.284668, 
                    -28.844674
                ], 
                [ 
                    -53.4375, 
                    -33.742613
                ]
            ]
        ]
    }
}

Comment by J Rassi [ 05/Mar/15 ]

Hi,

I'll need additional information to further diagnose this issue:

  • Could you provide the output of running this query with explain(true)?
  • Could you provide an example document that this query does not return, which you would expect it to return?

Thanks.
~ Jason Rassi

Generated at Thu Feb 08 03:44:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.