[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: |
|
||||||||
| Description |
|
the index is set with : ) 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"); DBCollection coll = database.getCollection("offerings"); 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 { |
| 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:
| ||
| Comment by Jeffrey Yemin [ 22/Oct/12 ] | ||
|
Linking to | ||
| 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:
| ||
| Comment by Bruce Ellacott [ 22/Oct/12 ] | ||
|
that's version mongodb-osx-x86_64-2.2.0 and Java 1.6 |