Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom near query not working #65

Open
joshkopecek opened this issue May 31, 2017 · 5 comments
Open

Custom near query not working #65

joshkopecek opened this issue May 31, 2017 · 5 comments

Comments

@joshkopecek
Copy link

I'm having a problem with the custom near query again, using v4.4.1
If I specify a name for the query like so:

custom: {
    
    near: 'loc'
}

It throws an error from Mongo:

error processing query: ns=echoesfullstack-dev.collections limit=10Tree: $and    pub_status == \"public\"
    GEONEAR  field=(query, point) => {
  const pointArr = point.split(',').map(p => parseFloat(p, 10));

  if (pointArr.length >= 2) {
    if (!isNaN(pointArr.reduce((a, b) => a + b))) {
      const max = pointArr[2];
      const min = pointArr[3];

      query[field] = {
        $near: {
          $geometry: {
            type: 'Point',
            coordinates: pointArr.splice(0, 2),
          },
        },
      };

      if (!isNaN(max)) {
        query[field].$near.$maxDistance = max;

        if (!isNaN(min)) {
          query[field].$near.$minDistance = min;
        }
      }
    }
  }
} maxdist=1.79769e+308 isNearSphere=0
Sort: {}
Proj: {}
 planner returned error: unable to find index for $geoNear query

It seems to be returning the entire function as a query. Is that expected behaviour?

I manually overrode the query with :

if (query.near) {
    let pointArr = req.query.near.split(',').map(p => parseFloat(p, 10));
    let n = {
      $near: {
        $geometry: {
          type: 'Point',
          coordinates: pointArr.splice(0, 2),
        }
      }
    };
    // maybe we have $maxDistance
    if (pointArr.length)
      n.$near.$maxDistance = parseFloat(pointArr.shift());
    delete query.near
    query.loc = n;
  }

and it now works fine for me.

@Starefossen
Copy link
Contributor

Hmmm... that's odd. Take a look at the unit test for near and you can see that it is supposed to only return a JSON output.

The error also says that mongoDB is unable to find a geospatial index for the $geoNear query. Have you added the index to the loc field?

@joshkopecek
Copy link
Author

Definitely have an index for the loc, since the subsequent manually-created query works fine. Also was working up until I upgraded to 4.x (AFAIK).

I will check the unit tests and report again.

@Starefossen
Copy link
Contributor

Thanks. Also, just try to run this:

const mqs = new MongoQS({
  custom: {
    near: 'loc',
  },
});

console.log(mqs.parse({ loc: '1,2' }));

@joshkopecek
Copy link
Author

joshkopecek commented Jun 1, 2017 via email

@Starefossen
Copy link
Contributor

No problem, glad we could sort this out. Please report back if you find any errors with the code so we can fix them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants