Hello there.
Can somebody post the java equivalent of this query please?
db.users.aggregate([ { "$geoNear": { "near": { "type": "Point", "coordinates": [ 17.487652, 78.385807 ] }, "maxDistance": 5000, "spherical": true, "distanceField": "distance" }} ]).pretty()
Hello @manlan, welcome to the community.
You can use the MongoDB Compass’s Aggregation Pipeline Builder to build an aggregation query and then Export Pipeline to Specific Language - to Java.
Also, see the Java Driver Tutorials on Aggregation and Geospatial Search
Thank you for the welcome!
Oh this is great! I’ll check it out, thanks a ton!
I tried the above suggested approach.
My input aggregation is this:
{ "near": { "type": "Point", "coordinates": [ 17.487652, 78.385807 ] }, "maxDistance": 5000.0, "spherical": true, "distanceField": "distance" }
It gave me Java code which is equivalent to this Kotlin code:
var stage = listOf(
eq("\$geoNear", and(
eq("near", and(
eq("type","Point"),
eq("coordinates",listOf(17.487652,78.385807)))),
eq("maxDistance", 5000.0),
eq("spherical", true),
eq("distanceField", "distance"))))
collection.aggregate(stage)
But I get this error
'$geoNear requires a 'near' option as an Array' on server
Also, I see in the logs that my application code above is turning into this during runtime:
"pipeline": [
{"$geoNear":
{"$and":
[{"near":
{"$and": [{"type": "Point"}, {"coordinates": [17.487652, 78.385807]}]}
},
{"maxDistance": 5000.0},
{"spherical": true},
{"distanceField": "distance"}]
}
}
]
Can you help me understand what’s going wrong?
I tried your aggregation pipeline:
[
{
$geoNear: {
"near": { "type": "Point", "coordinates": [ 17.487652, 78.385807 ] },
"distanceField": "distance"
"maxDistance": 5000.0,
"spherical": true,
}
}
]
It works fine when run from the Compass (v 1.21.2, MongoDB v4.2.8), the mongo
shell and the Java code (Java driver 3.12.2).
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.