[SERVER-54105] $geoNear in $lookup with dynamic coordinates from "parent" collection Created: 28/Jan/21  Updated: 06/Dec/22  Resolved: 02/Feb/21

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

Type: Bug Priority: Major - P3
Reporter: Dimitris Papatsarouchas Assignee: Backlog - Query Team (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-34766 Allow $expr or $$field in the $geoNea... Closed
Assigned Teams:
Query
Operating System: ALL
Participants:

 Description   

Hello,

I'm trying to get nearest points from joined collection like this:

db.collectionA.aggregate(
    [
        {
            $geoNear: {
            near: { "type": "Point", "coordinates": [-4.4657183, 48.38249] },
            distanceField: "calcDistance",
            maxDistance: 10,
            key: "geometry",
            includeLocs: "dist.geometry",
            spherical: "true"
            }
        },
        {$limit: 100},
        {
            $lookup: {
                from: "collectionB",
                let: {
                    plon: {"$arrayElemAt":["$geometry.coordinates",0]},
                    plat: {"$arrayElemAt":["$geometry.coordinates",1]},
                },
                pipeline: [
                    {
                    $geoNear:{
                        "near": {type: "Point", coordinates:
                            ["$$plon","$$plat"],
                        },
                        key: "geometry",
                        maxDistance: 10000,
                        distanceField: "dist.calculated",
                        spherical: "true"
                    }
                    },
                ],
                as: 'joinedGeometry'
            }
        }
    ]
)

So i need for every point in collectionA to find nearest points in the collectionB.

On the specified aggregation im recieving the following error:

geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 10000.0

I tried to remove that field and the following message:

invalid argument in geo near query: type

I also tried to specify in the let section the numbers like this:

let: {
    plon: -3.4032917,
    plat: 46.91752,
}

But with the same results

 

Thanks



 Comments   
Comment by Dimitris Papatsarouchas [ 29/Jan/21 ]

Hello Edwin Zhou

Thank you for your help !

 

Best regards,

Dimitris

Comment by Edwin Zhou [ 28/Jan/21 ]

Hi d.papatsarouchas@gmail.com,

Thank you for providing extra details and clarifications. I was successful in reproducing your issue. It appears that using the variables specified by let in the coordinates field of $geoNear in the $lookup stage pipeline will cause an error to be thrown. We're assigning this ticket to the appropriate team to be further investigated.

Best,
Edwin

Comment by Dimitris Papatsarouchas [ 28/Jan/21 ]

Hello Edwin Zhou

Thank you for your answer,

I have both collections indexed with 2dsphere on the geometry field, I'm sorry I forgot to mention that.

Also the collectionA is sharded and collection B is not so I can use $lookup.

I just tried the same query on an unsharded mongo:4.4.3 and mongo:4.4.2 and I have the same error message.

I'm running mongo in Docker.

 

Best regards,

Dimitris

 

— EDIT

This comes up from mongo shell:

uncaught exception: Error: command failed: {uncaught exception: Error: command failed:
{ "ok" : 0, "errmsg" : "geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 10000.0", "code" : 2, "codeName" : "BadValue"} : aggregate failed :_getErrorWithCode@src/mongo/shell/utils.js:25:13doassert@src/mongo/shell/assert.js:18:14_assertCommandWorked@src/mongo/shell/assert.js:618:17assert.commandWorked@src/mongo/shell/assert.js:708:16DB.prototype._runAggregate@src/mongo/shell/db.js:266:5DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12@(shell):1:1

 

Comment by Edwin Zhou [ 28/Jan/21 ]

Hi d.papatsarouchas@gmail.com,

I was able to successfully reproduce your issue and reached the same error. I was also able to work around that error by creating a 2dsphere index on collectionB on the geometry field:

db.collectionB.createIndex({geometry: "2dsphere"})

I was able to query the document without errors.

Can you try to run the same query after creating the index on the from field of $lookup?

Best,
Edwin

Generated at Thu Feb 08 05:32:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.