I want to run a $near query on a collection through Java. I am not sure how to use QeuryBuilder or BasicDbObject for this. What is the correct way to run $near query through Java code. Below is my code for document structure. "location" attribute stores type as point and coordinates stores the lat-long. I have created a 2dsphere index on this collection.
BasicDBObject doc = new BasicDBObject("attr1", nextLine[0])
.append("attr2", nextLine[1])
.append("edge-metro-code", nextLine[6])
.append("location", new BasicDBObject("type", "Point")
.append("coordinates",latLong))
.append("attr3", nextLine[9])
.append("attr4", nextLine[10])
First you'll need a maxDistance and a referential point to calculate near documents. The code bellow shows how to build a DBObject to query near documents.
This will result in that json:
If you're using mongodb 2.2, the code above will not work. I have to use the following:
The json will be:
You can find more informations about near queries here:
http://docs.mongodb.org/manual/reference/operator/near/