question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

near()'s maxDistance seems to be being ignored

See original GitHub issue

Hey there,

I’m trying to write a simple query in Mongoose that returns a number of nearby results that all have GeoJSON properties. However, I’m getting a different number of results from my Mongoose script to that of a query directly in mongo.

Here’s a raw Mongo script:

var conn = new Mongo(),
    db = conn.getDB("my_db"),
    collection;

collection = db.my_moodels.find({
  geo: {
    $nearSphere: {
      $geometry: {
        type: "Point",
        coordinates: [-0.02028, 51.50703]
      },
      $maxDistance: 3000 // 3k I'm assuming
    }
  }
});

print(collection.length()); // I get 6 (which is round about right)

Here’s my Mongoose script:

MyModel
  .find()
  .where('geo')
  .near({
    center: [-0.02028, 51.50703],
    maxDistance: 3000,
    spherical: yes
  })
  .exec(function (err, results) {
    if (err) {
      throw err;
    } else {
      console.log(results.length); // Here I get over 340, which I think is all of the possible results.
    }
  });

As you can see, the result count is entirely different. In fact, changing the maxDistance property in the Mongoose script seems to do nothing.

I’ve also found that putting the query directly in to the find() method brings back the correct amount of results:

MyModel
  .find({
    geo: {
      $nearSphere: {
        $geometry: {
          type: "Point",
          coordinates: [-0.02028, 51.50703]
        },
        $maxDistance: 3000
      }
    }
  })
  .exec(function (err, results) {
    if (err) {
      throw err;
    } else {
      console.log(results.length); // Now I get 6
    }
  });

Is there a problem with using the near() method or am I using it wrong?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:13

github_iconTop GitHub Comments

2reactions
ghostcommented, Jul 25, 2016

Confirmed nothing wrong with $near or $maxDistance. The problem was elsewhere in my code. Sorry! 🐑

0reactions
ghostcommented, Jul 23, 2016

Thanks for pointing that out. I’ll try that now. However, the query examples for geoJSON data with 2dsphere indexes do show $maxDistance as a sub-key of $near. https://docs.mongodb.com/manual/reference/operator/query/near/#query-on-geojson-data

After fooling around in the past few days, I am now almost completely sure the problem is in my code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

$geoNear aggregation ignoring maxDistance - Stack Overflow
I solved it with following query: $geoNear: { near: { type: 'Point', coordinates: [lng, lat] }, distanceField: 'dist', limit: limit, ...
Read more >
chrony.conf(5) Manual Page - TuxFamily
When a pool source is unreachable, marked as a falseticker, or has a distance larger than the limit set by the maxdistance directive,...
Read more >
How to Deal With Being Ignored | Relationship Coach Online
1. Your partner may simply need some space to collect their thoughts and deal with their own emotions. Give them time and work...
Read more >
Scripting API: Physics.Raycast - Unity - Manual
The max distance the ray should check for collisions. layerMask, A Layer mask that is used to selectively ignore Colliders when casting a...
Read more >
Why Being Ignored Hurts So Much | Psychology Today
Research finds that feeling ignored can affect people's sensory perceptions, such as feeling that surroundings seem quieter.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found