[JAVA-671] Geospatial Query with implied and returns wrong results Created: 21/Oct/12  Updated: 22/Oct/12  Resolved: 22/Oct/12

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Bruce Ellacott Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OS X Mountain Lion


Issue Links:
Related
related to JAVA-672 JSON.parse should throw an exception ... Closed

 Description   

the index is set with :
db.offerings.ensureIndex(

{ coord : "2d", categoryId: 1 }

)

while the shell query returns 1 document :

db.offerings.find( { coord :

{ $near : [-122,37] }

, categoryId : '212' } );

the java version returns two including one with categoryId not equal 212 (212 shows in the debugger as the value of cat)

String lat = (String) this.getRequest().getAttributes().get("lat");
String lon = (String) this.getRequest().getAttributes().get("long");
String cat = (String) this.getRequest().getAttributes().get("cat");

DBCollection coll = database.getCollection("offerings");
Double locationLongitude = null;
Double locationLatitude = null;

try

{ locationLongitude = Double.valueOf(lon.trim()).doubleValue(); locationLatitude = Double.valueOf(lat.trim()).doubleValue(); }

catch (NumberFormatException e)

{ System.out.println("NumberFormatException: " + e.getMessage()); }

DBCursor cur = coll.find(new BasicDBObject("coord",JSON.parse("{$near : [ " + locationLongitude + "," + locationLatitude + " ]}, categoryId : " + cat)));

try {
JSONObject offeringAsJson = new JSONObject();
while(cur.hasNext()) {
DBObject obj = cur.next();
System.out.println("\nGetNext value for categoryId:"+ obj.get("categoryId"));



 Comments   
Comment by Bruce Ellacott [ 22/Oct/12 ]

Thanks for your help with the construction. I spent a lot of time trying to get it to work. I assumed if it got through the parsing it was a valid statement.

Comment by Jeffrey Yemin [ 22/Oct/12 ]

Also, in general it's safer to use the QueryBuilder class instead of parsing JSON. It would look something like:

coll.find(QueryBuilder.start("coord").near(Double.valueOf(lon.trim()), Double.valueOf(lat.trim())).
                         and("categoryId").is(cat).get());

Comment by Jeffrey Yemin [ 22/Oct/12 ]

Linking to JAVA-672: JSON.parse should throw an exception if the inut string is not fully consumed.

Comment by Jeffrey Yemin [ 22/Oct/12 ]

There's a couple of problems. For one, the String cocatenation in the call to JSON.parse() isn't quoting the category, so it ends up looking like: "{$near : [ 37.0,-122.0 ]}, categoryId : 212". But the reason you're getting results where category is not equal to 212 is because JSON.parse is silently ignoring everything after the last curly brace, when it should probably throw an exception, as the string is not a fully formed JSON document. This should work better:

  DBCursor cur = coll.find((DBObject) JSON.parse(
       "{ coord : {$near : [ " + locationLongitude + "," + locationLatitude + " ]}, categoryId : '" + cat + "'}"));

Comment by Bruce Ellacott [ 22/Oct/12 ]

that's version mongodb-osx-x86_64-2.2.0 and Java 1.6

Generated at Thu Feb 08 08:52:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.