Home > Mobile >  MongoDB "$geoNear accepts just one argument" and "invalid argument 'type'&q
MongoDB "$geoNear accepts just one argument" and "invalid argument 'type'&q

Time:12-01

I am having a problem with $geoNear that it is throwing errors no matter what I do. Errors that make no sense.

const location = [Number(response.data.results[0].location.lat), Number(response.data.results[0].location.lng)];
const distance = parseFloat(req.query.distance);

console.log(location); // [ 37.620321, -92.139884 ]
console.log(distance); // 40233.5

return Vendor.aggregate([
    {$geoNear: {
        near: {
            type: "Point",
            coordinates: location
        },
        distanceField: "distance",
        maxDistance: distance
    }},
    {$match: {"publicData.searchable": true}},
    {$project: {
        name: 1,
        description: 1,
        items: 1,
        address: 1,
        email: 1,
        distance: 1,
        publicData: 1,
        url: 1
    }}
]);

This will give the error:

MongoServerError: geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 40233.5
    at Connection.onMessage (/home/leemorgan/projects/javascript/market/market-api/node_modules/mongodb/lib/cmap/connection.js:207:30)
    at MessageStream.<anonymous> (/home/leemorgan/projects/javascript/market/market-api/node_modules/mongodb/lib/cmap/connection.js:60:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (/home/leemorgan/projects/javascript/market/market-api/node_modules/mongodb/lib/cmap/message_stream.js:132:20)
    at MessageStream._write (/home/leemorgan/projects/javascript/market/market-api/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:391:12)
    at _write (node:internal/streams/writable:332:10)
    at MessageStream.Writable.write (node:internal/streams/writable:336:10)
    at Socket.ondata (node:internal/streams/readable:754:22)
    at Socket.emit (node:events:513:28) {
  ok: 0,
  code: 2,
  codeName: 'BadValue',
  [Symbol(errorLabels)]: Set(0) {}
}

I found some information that this error can be throw when the data for "near" is bad. However, I have checked and my data is formatted fine.

I then tried simple removing the "maxDistance" field to see happens and I get this:

MongoServerError: invalid argument in geo near query: type
    at Connection.onMessage (/home/leemorgan/projects/javascript/market/market-api/node_modules/mongodb/lib/cmap/connection.js:207:30)
    at MessageStream.<anonymous> (/home/leemorgan/projects/javascript/market/market-api/node_modules/mongodb/lib/cmap/connection.js:60:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (/home/leemorgan/projects/javascript/market/market-api/node_modules/mongodb/lib/cmap/message_stream.js:132:20)
    at MessageStream._write (/home/leemorgan/projects/javascript/market/market-api/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:391:12)
    at _write (node:internal/streams/writable:332:10)
    at MessageStream.Writable.write (node:internal/streams/writable:336:10)
    at Socket.ondata (node:internal/streams/readable:754:22)
    at Socket.emit (node:events:513:28) {
  ok: 0,
  code: 2,
  codeName: 'BadValue',
  [Symbol(errorLabels)]: Set(0) {}
}

This makes no sense to me either. Can anybody tell me what is going wrong here that is creating all of these issues?

CodePudding user response:

the location in the database should be in format of location : [ lon, lat ] which was the main issue. as mongodb does not validate location array this format.

Verify both the data in DB and points specified while querying both should be correct format.

  • Related