Spring data mongo generates wrong query for geo query with $near

While working with Spring data mongo,  I had to search a collection using geo spatial query. I needed to find all items from a point within a radius.

Method in repository interface looks like:

Slice findByLocationNear(Point source, Distance distance, Pageable pageable);

Generated query filter is:

{ location: { $near: { x: 56.6474, y: 28.63752 }, $maxDistance: 0.01799856011519078 } }

But mongo expect longitude as the first property in $near query value, without considering property name(!).

And the query does not get expected result.

To solve the problem, right now I am using a way around. While constructing Point object, setting longitude first. Then the query returns expected result.

Point source = new Point(lng, lat);
repository.findByLocationNear(source, distance, pageable);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s