Spring -data mongodb geo query

I use spring -data to communicate with mongodb, I’m looking for a way to execute a geo-request that will be retrieved next to the profiles with a given geodata point from the database, my request requirements:

1) distance limit
2) limit on the number of returned profiles
3) additional time search of the field in the document profile
4) the ability to include / exclude fields from received documents

At first I used the mongoTemplate.geoNear method ...

Query criteria = new Query(Criteria.where("time").gte("some_date")); criteria.fields().exclude("friends"); NearQuery query = NearQuery.near(point).maxDistance(maxDistance) .num(limit).query(criteria); GeoResults<Profile> result = mongoTemplate .geoNear(query, Profile.class);<br/> 

But then I realized that mongo does not support the inclusion / exclusion of fields in geoNear queries.
So I think about the Near query, it seems that the only way to execute such a β€œcomplex” query in spring-data is using the @Query annotation , is there any way to support the requirements 2,3 above in this kind of query? any other way?
Thanks

+4
source share
1 answer

You doubt that in fact you can find fragments of all the answer you need. You only need to use query.addCriteria to put them all together.

1: Use a circle with a radius of 2. Use Query.limit (count)
3. Use Query.where ("date"). Gte ()
4. Use the fields for project results

Here you go

  Point point = new Point(6.12343, 3.121211); Distance distance = new Distance(100, Metrics.KILOMETERS); Circle circle = new Circle(point, distance); Integer resultLimit = 20; //Limit to 20 records Criteria geoCriteria = Criteria.where("position").withinSphere(circle); Criteria timeCriteria = Criteria.where("time").gte(new Date()); Query query = Query.query(geoCriteria); query.addCriteria(timeCriteria); query.fields().exclude("friends"); mongoTemplate.find(query, Profile.class); 
+1
source

Source: https://habr.com/ru/post/1435690/


All Articles