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' ,
},
},
],
);