简体   繁体   中英

MongoDB $near query using Java driver

I have some shops with products on them in my MongoDB db following this data model: Shop { idShop, name, location {lat, lon}, products [] of int } If I run the following command in the mongoDB default console:

db.shops.find({products:1, location:{$near:[41.391204,2.145381]}}).limit(300)

to find the shops that have product nº 1 ordered by distance I get the following results:

{ "_id" : ObjectId("4f410cefe4b03a0e3cff6f5a"), "name" : "Shop1", "location" : { "lat" : 41.389915, "lon" : 2.135628 }, "products" : [ 1, 5, 4 ] }

{ "_id" : ObjectId("4f410cefe4b03a0e3cff6f59"), "name" : "Shop2 Center", "location" : { "lat" : 41.388191, "lon" : 2.128816 }, "products" : [ 1, 2, 3 ] }

{ "_id" : ObjectId("4f410cefe4b03a0e3cff6f5c"), "name" : "Shop3", "location" : { "lat" : 41.384712, "lon" : 2.172031 }, "products" : [ 6, 1 ] }

{ "_id" : ObjectId("4f410cefe4b03a0e3cff6f5d"), "name" : "Shop4", "location" : { "lat" : 41.384029, "lon" : 2.173936 }, "products" : [ 6, 1, 2, 3, 4, 5 ] }

Now, I want the same results but using my java servlet. If I search by only product number, the results are OK but not sorted. Then, if I try to sort them by adding a new key/value condition it doesn't return nothing. The code I'm using is the following:

BasicDBObject query = new BasicDBObject();

query.put("products", Integer.parseInt(idProduct));

QueryBuilder query2 = new QueryBuilder();

query2.near(Double.parseDouble(lat), Double.parseDouble(lon));

query.putAll(query2.get());

curShops = collShops.find(query);

This returns an empty JSON. I've also tryed other ways like:

BasicDBObject query = new BasicDBObject();

query.put("products", Integer.parseInt(idProduct));

ArrayList ar = new ArrayList(2);

ar.add(Double.parseDouble(lat));

ar.add(Double.parseDouble(lon));

query.put("location", new BasicDBObject("$near", ar));

curShops = collShops.find(query);

Without any luck.

Can anyone help me with this?

Thanks in advance.

I dont have any experience with java driver, but it seem to me the problem is in this line

 query2.near(Double.parseDouble(lat), Double.parseDouble(lon));

Accoridng to mongodb documentation,

The code assumes that you are using decimal degrees in (longitude, latitude) order. This is the same order used for the GeoJSON spec. Using (latitude, longitude) will result in very incorrect results, but is often the ordering used elsewhere, so it is good to double-check

Also from the java mongodb-api-doc , you can see

near

public QueryBuilder near(double x,
                         double y)

Equivalent of the $near operand

Parameters:

x - x coordinate

y - y coordinate

x-cordinate denotes Longitude here. So the location co-ordinates must be passed as [lon,lat] instead [lat,lon]

Change your query to

 query2.near(Double.parseDouble(lon), Double.parseDouble(lat));

to make it work

Yes, this is pretty straight forward. I have just posted an example that should walk you through the solution at http://chuckjohnson.wordpress.com/ - see http://chuckjohnson.wordpress.com/2012/04/02/geospatial-location-based-searches-in-mongodb-part-2-simple-searching/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM