Skip to content

Unable to make a count on a query with a GeoConstraint #5285

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

Closed
jlnquere opened this issue Jan 9, 2019 · 2 comments
Closed

Unable to make a count on a query with a GeoConstraint #5285

jlnquere opened this issue Jan 9, 2019 · 2 comments
Labels
type:bug Impaired feature or lacking behavior that is likely assumed

Comments

@jlnquere
Copy link
Contributor

jlnquere commented Jan 9, 2019

Issue Description

When I try to make a count on a query with a GeoConstraint, I have an internal server error. It seems to be tied to an issue with Mongo: MongoError: $nearSphere is not allowed inside of a $match aggregation expression.

Steps to reproduce

I built a minimal parse instance (just a copy paste of documentation):

var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var app = express();

var api = new ParseServer({
  databaseURI: 'mongodb://localhost:27017/minimal_parse', // Connection string for your MongoDB database
  appId: 'myAppId',
  masterKey: 'myMasterKey',
  serverURL: 'http://localhost:1337/parse',
});

// Serve the Parse API on the /parse URL prefix
app.use('/parse', api);

app.listen(1337, function() {
  console.log('parse-server-example running on port 1337.');
});

On client side:

var Parse = require('parse/node');

const parseAppId = 'myAppId';
const parseMasterKey = "myMasterKey";
const parseServerURL = 'http://localhost:1337/parse';

Parse.serverURL = parseServerURL;
Parse.initialize(parseAppId, undefined, parseMasterKey);

let run = async () => {
  let t = new Parse.Object("Contact");
  t.set("geocode", new Parse.GeoPoint(0,0))
  t.set("tel", "33663192122")
  t.set("name", "test")
  await t.save({}, {useMasterKey:true})


  let query = new Parse.Query('Contact').withinKilometers("geocode", new Parse.GeoPoint(0, 0), 20)

  let items = await query.find()
  // "Got 1 items with find()"
  console.log("Got "+ items.length+" items with find().");  

  let itemsCount = await query.count()

  // Expecting "Got 1 items with find()", but got an error 
  console.log("Got "+ itemsCount+" items with count().");  
}

run()

Expected Results

I'm expecting query.count() to return a count.

Actual Outcome

Instead, the client got an internal server error. On server, I got this trance:

The request (so far, so good):

verbose: REQUEST for [GET] /parse/classes/Transporter: {
  "where": {
    "geocode": {
      "$nearSphere": {
        "__type": "GeoPoint",
        "latitude": 0,
        "longitude": 0
      },
      "$maxDistance": 0.0031392246115209545
    }
  },
  "limit": 0,
  "count": 1
} method=GET, url=/parse/classes/Transporter, user-agent=node-XMLHttpRequest, Parse/js2.1.0 (NodeJS 11.1.0), accept=*/*, content-type=text/plain, host=localhost:1337, content-length=270, connection=close, __type=GeoPoint, latitude=0, longitude=0, $maxDistance=0.0031392246115209545, limit=0, count=1

The error:

error: Uncaught internal server error. { MongoError: $nearSphere is not allowed inside of a $match aggregation expression
    at queryCallback (/Users/onejjy/Documents/Code/tmp/minimal_parse/node_modules/mongodb-core/lib/cursor.js:248:25)
    at /Users/onejjy/Documents/Code/tmp/minimal_parse/node_modules/mongodb-core/lib/connection/pool.js:532:18
    at process.internalTickCallback (internal/process/next_tick.js:70:11)
  ok: 0,
  errmsg:
   '$nearSphere is not allowed inside of a $match aggregation expression',
  code: 16426,
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {} } MongoError: $nearSphere is not allowed inside of a $match aggregation expression
    at queryCallback (/Users/onejjy/Documents/Code/tmp/minimal_parse/node_modules/mongodb-core/lib/cursor.js:248:25)
    at /Users/onejjy/Documents/Code/tmp/minimal_parse/node_modules/mongodb-core/lib/connection/pool.js:532:18
    at process.internalTickCallback (internal/process/next_tick.js:70:11)
MongoError: $nearSphere is not allowed inside of a $match aggregation expression
    at queryCallback (/Users/onejjy/Documents/Code/tmp/minimal_parse/node_modules/mongodb-core/lib/cursor.js:248:25)
    at /Users/onejjy/Documents/Code/tmp/minimal_parse/node_modules/mongodb-core/lib/connection/pool.js:532:18
    at process.internalTickCallback (internal/process/next_tick.js:70:11)

Environment Setup

  • Server

    • parse-server version: 3.1.3
    • Operating System: Tested on Mac OS and Ubuntu
    • Hardware: personal computer and production server
    • Localhost or remote server?: local and remote
  • Database

    • MongoDB version: 3.2.1
    • Storage engine: [FILL THIS OUT]
    • Hardware: personal computer and production server
    • Localhost or remote server? local and remote
@jlnquere
Copy link
Contributor Author

jlnquere commented Jan 9, 2019

If it can help:

I used some dichotomy and ran my minimal Parse server on top of different Parse Server versions. The issue seems to be introduced by the 3.1.0 version.

Indeed, with parse-server<=3.0.0, the .count() works as expected. The error is only happening with parse-server=>3.1.0.

@jlnquere
Copy link
Contributor Author

jlnquere commented Jan 9, 2019

I also sent a PR with a test that cover this issue: #5286

jlnquere added a commit to jlnquere/parse-server that referenced this issue Jan 16, 2019
@dplewis dplewis added type:bug Impaired feature or lacking behavior that is likely assumed pr-submitted labels Feb 5, 2019
UnderratedDev pushed a commit to UnderratedDev/parse-server that referenced this issue Mar 21, 2020
…ity#5285) (parse-community#5286)

* Add a tests that fails due to issue parse-community#5285

* Make test code much simpler

* Fix parse-community#5285 by rewriting query (replacing $nearSphere by $geoWithin)

All credit goes to @dplewis !

* move logic to transform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Impaired feature or lacking behavior that is likely assumed
Projects
None yet
Development

No branches or pull requests

2 participants