...
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
JIRAUSER1258456 commented on Fri, 29 Jan 2021 23:09:01 +0000: Hello Edwin Zhou Thank you for your help ! Best regards, Dimitris JIRAUSER1257066 commented on Thu, 28 Jan 2021 20:26:56 +0000: 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 JIRAUSER1258456 commented on Thu, 28 Jan 2021 18:35:14 +0000: 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 JIRAUSER1257066 commented on Thu, 28 Jan 2021 17:19:41 +0000: 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