Search code examples
javamongodbmongo-javamongo-java-driver

Find near query MongoDB with Java


I need to "translate",in java, this shell query (MongoDB):

db.runCommand( { geoNear: 'test', near: {type: "Point", coordinates: [115.0. 12.0]}, spherical: true, maxDistance: 40000}) 

I use mongo-java-driver:3.1.0, i test this query with shell and works fine.

Document:

{id: "test", "geometry": { "type": "Point", "coordinates": [115.2, 12.2]}}


Solution

  • I write and BasicDBObject that construct the query:

    BasicDBObject queryOnlyFind = new BasicDBObject("geometry", new BasicDBObject ("$near", new BasicDBObject("type","Point").append("coordinates",new double[] {0,0}) ) );

    Then: Collection.find(queryOnlyFind) and the result is the list of all documents.