33771

$near query through Java

Question:

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])

Answer1:

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.

double[] coords = new double[2]; long distance = 100; DBObject query = BasicDBObjectBuilder.start() .push("location") .add("$maxDistance", distance) .push("$near") .push("$geometry") .add("type", "Point") .add("coordinates", coords) .get();

This will result in that json:

{ "location": { "$maxDistance": 100, "$near": { "$geometry": { "type": "Point", "coordinates": [ 0, 0 ] } } } }

If you're using mongodb 2.2, the code above will not work. I have to use the following:

double[] coords = new double[2]; long distance = 100; DBObject query = BasicDBObjectBuilder.start() .push("location") .add("$maxDistance", distance) .add("$near", coords) .get();

The json will be:

{ "location" : { "$maxDistance" : 100, "$near" : [ 0, 0 ] } }

You can find more informations about near queries here:

<a href="http://docs.mongodb.org/manual/reference/operator/near/" rel="nofollow">http://docs.mongodb.org/manual/reference/operator/near/</a>

Recommend

  • Parsing xml with StAX: not getting a large content tag
  • How to use client.query without JSX components in Apollo Client?
  • MongoDB+Java: Credentials missing in the user document
  • Setting parent key but not child key while using endpoint-proto-datastore
  • Lat-Long processing in mongoDB
  • How to use $in operator in mongodb with two fields in java
  • How to retrieve / find all elements of a nested array in MongoDB Java
  • $near requires geojson point, given geojson point
  • Creating desktop applications for Windows RT?
  • How to convert a Java object to an XML string, and conversely convert XML to a Java object?
  • Java: How to insert a hashmap into MongoDB?
  • How does Skobbler prioritize which POIs to show?
  • Hatch area using pcolormesh in Basemap
  • Update Google Maps traffic layer without page reloading
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • Using a canvas object in a thread to do simple animations - Java
  • uniform generation of points on 3D box
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to attach a node.js readable stream to a Sendgrid email?
  • Play WS (2.2.1): post/put large request
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Highlight one bar in a series in highcharts?
  • How to access EntityManager inside Entity class in EJB3
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Android screen density dpi vs ppi
  • How would I use PHP exceptions to define a redirect?
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to extract text from Word files using C#?
  • JSON with duplicate key names losing information when parsed
  • vba code to select only visible cells in specific column except heading
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Proper way to use connect-multiparty with express.js?
  • Free memory of cv::Mat loaded using FileStorage API
  • need help with bizarre java.net.HttpURLConnection behavior
  • unknown Exception android
  • How to Embed XSL into XML
  • Conditional In-Line CSS for IE and Others?