Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-67149

$geoNear in $lookup with dynamic coordinates from "parent" collection

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.4.14, 4.4.2, 5.0.9
    • Component/s: None
    • Labels:
      None
    • Environment:
      Mac book pro 11.6.6
      Docker desktip 4.4.2
    • ALL
    • Hide
      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',
                },
              },
            ],
          ); 
      Show
      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' , }, }, ], );

      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.

            Assignee:
            edwin.zhou@mongodb.com Edwin Zhou
            Reporter:
            xavius@tutanota.com Xavius H.
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: