[SERVER-67149] $geoNear in $lookup with dynamic coordinates from "parent" collection Created: 09/Jun/22  Updated: 06/Jul/22  Resolved: 06/Jul/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 4.4.14, 4.4.2, 5.0.9
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Xavius H. Assignee: Edwin Zhou
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Mac book pro 11.6.6
Docker desktip 4.4.2


Attachments: PNG File Capture d’écran 2022-06-09 à 10.37.33.png     PNG File Capture d’écran 2022-06-09 à 10.39.49.png    
Issue Links:
Duplicate
duplicates SERVER-34766 Allow $expr or $$field in the $geoNea... Closed
Operating System: ALL
Steps To Reproduce:

db.space.aggregate(
      [
        { $match: { ref: "S1810697" } },
        {
          $lookup: {
            from: 'space',
            let: { coordinates: 'location.geometry.coordinates' },
            pipeline: [
              {
                $geoNear: {
                  near: { type: 'Point', coordinates: '$$coordinates' },
                  distanceField: 'distance',
                  maxDistance: 100,
                  includeLocs: 'location.geometry',
                  spherical: true,
                },
              },
            ],
            as: 'spaces',
          },
        },
      ],
    ); 

or

db.space.aggregate(
      [
        { $match: { ref: "S1810697" } },
        {
          $lookup: {
            from: 'space',
            let: { 
              plon: {"$arrayElemAt":["$location.geometry.coordinates",0]},
              plat: {"$arrayElemAt":["$location.geometry.coordinates",1]},
            },
            pipeline: [
              {
                $geoNear: {
                  near: { type: 'Point', coordinates: ["$$plon","$$plat"] },
                  distanceField: 'distance',
                  maxDistance: 100,
                  includeLocs: 'location.geometry',
                  spherical: true,
                },
              },
            ],
            as: 'spaces',
          },
        },
      ],
    ); 

Get this error :

MongoServerError: geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 100.0    at Connection.onMessage (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:2:569420)
    at MessageStream.<anonymous> (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:2:567337)
    at MessageStream.emit (events.js:315:20)
    at c (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:284094)
    at MessageStream._write (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:282817)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at MessageStream.Writable.write (internal/streams/writable.js:303:10)
    at Socket.ondata (internal/streams/readable.js:719:22)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12) 

Then I remove $MaxDistance & I get :

MongoServerError: invalid argument in geo near query: type    at Connection.onMessage (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:2:569420)
    at MessageStream.<anonymous> (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:2:567337)
    at MessageStream.emit (events.js:315:20)
    at c (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:284094)
    at MessageStream._write (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:282817)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at MessageStream.Writable.write (internal/streams/writable.js:303:10)
    at Socket.ondata (internal/streams/readable.js:719:22)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12) 

I have the same error when I hard coding the coordinate values into let :

db.space.aggregate(
      [
        { $match: { ref: "S1810697" } },
        {
          $lookup: {
            from: 'space',
            let: { coordinates: [ 2.3552528000000166, 48.8680689 ] },
            pipeline: [
              {
                $geoNear: {
                  near: { type: 'Point', coordinates: '$$coordinates' },
                  distanceField: 'distance',
                 //maxDistance: 100,
                  includeLocs: 'location.geometry',
                  spherical: true,
                },
              },
            ],
            as: 'spaces',
          },
        },
      ],
    ); 

But its working when I don't use the var define into let :

db.space.aggregate(
      [
        { $match: { ref: "S1810697" } },
        {
          $lookup: {
            from: 'space',
            let: { coordinates: [ 2.3552528000000166, 48.8680689 ] },
            pipeline: [
              {
                $geoNear: {
                  near: { type: 'Point', coordinates: [ 2.3552528000000166, 48.8680689 ] },
                  distanceField: 'distance',
                 //maxDistance: 100,
                  includeLocs: 'location.geometry',
                  spherical: true,
                },
              },
            ],
            as: 'spaces',
          },
        },
      ],
    ); 

Participants:

 Description   

Hello,

I'm trying to get the nearest spaces from specific space.
To get that, I'm making a aggregation with a $lookup and a $geoNear in its pipeline then let a var to get the coordinates from the parent space.

The location field has a 2dsphere geospacial index.

But its not working.
After some searches, I found this issue, the same : https://jira.mongodb.org/browse/SERVER-54105
But it is marked as resolved.

I have tried with versions 4.4.2, 4.4.14 & 5.0.9.

Am I missing something or is it a MongoDB bug ?

Thank you.



 Comments   
Comment by Edwin Zhou [ 06/Jul/22 ]

Hi xavius@tutanota.com,

I appreciate your patience while we investigate this issue. The issue you linked, SERVER-54105, was closed as a duplicate for SERVER-34766, which describes an improvement that can be made for allowing variables, as defined in let, be used in the geoNear stage. I will close this as a duplicate of SERVER-34766 and you may follow that ticket for further developments.

Best,
Edwin

Comment by Xavius H. [ 10/Jun/22 ]

There is a typo in my description into my request :

let: { coordinates: 'location.geometry.coordinates' },

Instead of : 

let: { coordinates: '$location.geometry.coordinates' },

But it changes nothing for this issue.

Generated at Thu Feb 08 06:07:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.