Adding a "withinMiles" GeoPoint constraint to a compound Query - unable to find index for $geoNear query - Mongo DB error - Cloud Code / Javascript

363 views
Skip to first unread message

Max Tannone

unread,
Dec 4, 2016, 11:04:29 PM12/4/16
to back{4}app
First off, is it even allowed to do this:

(pseudo code below)

var userQueryOne = new Parse.Query(Parse.User);
userQueryOne.someConstraint1
userQueryOne.someConstraint2

var userQueryTwo = new Parse.Query(Parse.User);
userQueryTwo.someConstraint1
userQueryTwo.someConstraint2

var combinedInstallationQuery = Parse.Query.or(userQueryOne, userQueryTwo);

combinedInstallationQuery.withinMiles("lastLocation", itemGeopoint, 10);

Assuming the above line is allowed, when I run this query, I get this on the Server App Logs:

 [32minfo [39m: warning: error while sending push MongoError: error processing query: ns=ae6c919607ed4fefa0bfe1170e735acb._Installation limit=0 skip=0
Tree: $and
    $or
        _p_user $in
[ ]
        _p_user $in
[ "_User$LUbRhX9iY1" ]
    deviceType
== "ios"
    GEONEAR  field
=lastLocation maxdist=0.00252602 isNearSphere=1
Sort: {}
Proj: {}
 planner returned error
: unable to find index for $geoNear query
    at
Function.MongoError.create (/usr/src/app/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)
    at toError
(/usr/src/app/node_modules/mongodb/lib/utils.js:114:22)
    at
/usr/src/app/node_modules/mongodb/lib/collection.js:1008:67
    at commandCallback
(/usr/src/app/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1194:9)
    at
Callbacks.emit (/usr/src/app/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:119:3)
    at
null.messageHandler (/usr/src/app/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:358:23)
    at
Socket.<anonymous> (/usr/src/app/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:292:22)
    at emitOne
(events.js:77:13)
    at
Socket.emit (events.js:169:7)
    at readableAddChunk
(_stream_readable.js:153:18)

This seems to be the most descriptive line: 

 planner returned error: unable to find index for $geoNear query
I have also printed out my itemGeopoint variable, and it is ok (I removed my location for privacy purposes):

{ longitude: ###,
  latitude: ###,
  __type: 'GeoPoint' }
Please advice.

Thanks in advance.

Max Tannone

unread,
Dec 4, 2016, 11:09:09 PM12/4/16
to back{4}app
I wanted to add that "lastLocation" in the combinedInstallationQuery is a field that exists within my User class, which is what the 2 sub-queries are (queries on the User class).

Davi Macêdo

unread,
Dec 5, 2016, 8:42:36 AM12/5/16
to back{4}app
Hi, Max.

In order to run geo queries, you need a geo index in the column you are querying to.

Parse Server is supposed to automatically create those indexes but it is probably failing due to your complex query :)

Could you please tell me your app id, the class name and the column name you are querying? Send me to ad...@back4app.com

I will manually add the index to your database and report the problem to the community.

Best!

Max Tannone

unread,
Dec 5, 2016, 12:00:11 PM12/5/16
to back{4}app
Thanks Davi, I have sent you an email with the information requested.

Davi Macêdo

unread,
Dec 6, 2016, 1:22:46 AM12/6/16
to back{4}app
Hi, Max.

Just done.

Can you please try again and revert to me?

Best.

Max Tannone

unread,
Dec 7, 2016, 9:57:21 PM12/7/16
to back{4}app
Thanks Davi, 

It seems to be working with a static number in my geoquery. I'm having issues when trying to perform the geoquery within a for loop, having the radius set by the loop, and then continuing on to send a PFPush within this loop.

However, I think that these issues are separate from my original problem, so thank you! I have scheduled a follow-up Skype 


Davi Macêdo

unread,
Dec 8, 2016, 11:07:33 AM12/8/16
to back{4}app
Hi, Max.

No worries. Pick up a time slot in the link below.

Best!

Max Tannone

unread,
Dec 8, 2016, 7:50:05 PM12/8/16
to back{4}app
Thanks Davi, I reserved 4pm EST Friday, december 8.

Alexis L.

unread,
Jan 7, 2017, 5:57:33 AM1/7/17
to back{4}app
Same problem here, is there a work around?

Alexis L.

unread,
Jan 7, 2017, 6:18:38 AM1/7/17
to back{4}app
Using robomongo, I managed to re-index, seems that the index directive is taken into account, but never executed...
Can you please provide an official workaround?

Max Tannone

unread,
Jan 9, 2017, 6:39:24 PM1/9/17
to back{4}app
Alexis, I managed to get this working. Are you still having issues?

Alexis L.

unread,
Jan 10, 2017, 12:42:31 AM1/10/17
to back{4}app
Hi Max,
thanks for the helping hand :)

I've managed to trigger an index rebuild, on the Geopoint pointer. So it's apparently working, I'm not sure it'll always work though. What I'd like to hear from Back4app folks is: How do I track index consistency? what happens if the db stops building the  index ? Are there scaling effects I should be aware of? so on and so forth.

How do you manage this index yourself?

There's one thing for sure, index do break.

Davi Macêdo

unread,
Jan 11, 2017, 1:37:33 PM1/11/17
to back{4}app
Hi, Guys.

We have an index optimization tool in our database that run every day and optimize the most used and most slow queries.

Talking about 2d indexes (for geoqueries), that's another case. Parse Server is supposed to create those indexes if they are missing. But actually it seems to no be working for all cases.

We will address this issue soon and post a PR to the Parse Server open source community.

Meanwhile if you have any problem, just send me an e-mail to ad...@back4app.com and I will manually create the index for you.

Best.
Reply all
Reply to author
Forward
0 new messages