geolocation - MongoDb near/geonear query with variable distance -


I want to execute a query where the distance is a dynamic field from the collection.

Examples of entries in the archive:

  {name: 'myName', location: {lat: 10, lng: 20}, max guideline: 10} {name: 'MyName2', location: {lat: 20, lng: 20}, maximum distance: 100}   

My goal is to get all the elements from this collection, where it the location is close to a given place, we say (10,10) but where the distance calculation is less than this maximum distance field

I have one in the nearest query Constant value (i want all I can set up maximum distance in high enough to retrieve elements and then filter into my Java code but I would prefer to do it on query, maybe SQL oriented.

You will not be able to do this with a common query because you can not set dynamically the documents per distance As MongoDB 2.4 you can do it with the aggregation framework because they have added the geographical operator for the introduction of pipelines.

The first step would be GOAIR which is similar to the live command. We will get a distance from the point specified (10,10) as a point.

In the second step, we get the maximum operator distance from the project operator and maximum

In the end, we match those documents which have positive delta (maximum distance)> gt; 0 is.

Here pipeline using the helpful sections.

  package example; Import static com.allanbank.mongodb.builder.AggregationProjectFields.include; Import static com.allanbank.mongodb.builder.QueryBuilder.where; Import static com.allanbank.mongodb.builder.expression.Expressions.field; Import static com.allanbank.mongodb.builder.expression.Expressions.set; Import static com.allanbank.mongodb.builder.expression.Expressions.subtract; Import com.allanbank.mongodb.bson.element.ArrayElement; Import com.allanbank.mongodb.builder.Aggregate; Import com.allanbank.mongodb.builder.AggregationGeoNear; Import com.allanbank.mongodb.builder.GeoJson; Public category's overall GONER {public static zero major (string [] args) {gross total = gross. Builder () .Joyear (Aggregation Ganyoner Builder). Location (GeoJson.p (10, 10)) DistanceField ("Distance")). Subject ("name", "location", "maximum distance"), set ("delta", subtract (field ("maximum distance"), field ("distance")). Milan (where ("Delta.") .greaterThanOrEqualTo (0)) build (); System.out.println (New Arreelment ("Pipeline", Total .getPipeline ())); }}   

and the pipeline has been created here:

  pipeline: [{'$ geoNear': { Close: [10, 10], Distance: 'distance', spherical: false, unique docs: true}}, {'$ project': {name: 1, location: 1, maximum direction: 1, delta: {'$ Subtract ': [' $ MaxDistance ',' $ distance ']}}}, {' $ match ': {delta: {' $ gte ': 0}}}]   

- Rob.

PS The above builders are using a pre-release version of the 1.2.0 version of the driver. This code is going through construction matrix as I type and should be released on Friday, March 22, 2013.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -