How to write Mongo Geospatial query in PHP? - mongodb

I have a geospatial query Like Follwing Query.. This Query return 4 Records.. I want to Frame Following Query in PHP
db.master.find({ location:
{ $geoWithin:
{ $centerSphere: [ [ 94.60867254, 27.54018825 ], 10 / 6378.1 ] } } })
How to Frame above query in php? and get result?

You should check the Mongo documentation for PHP here and here

Related

MongoDB Aggregation to Spring Boot nested fields "$slice"?

Please see: Mongo Playground for the data and query.
Trying to figure out the equivalent SpringData MongoDB query for:
"idPlusVals": {
"values": {
"$slice": [
"$sa",
0,
4
]
}
}
Have tried the following but could not get the $slice in:
project().and("idPlusCount").nested(fields("totalCount"))
.and("idPlusVals").nested(bind("values", "sa"));

MongoDB $centerSphere vs Redis geospatial

I want to show nearby places on the map for a given user.
I can do so either by query MongoDB like this:
db.places.find( {
loc: { $geoWithin: { $centerSphere: [ [ -88, 30 ], 10/3963.2 ] } }
} )
and either by query a Reids geospatial set like this:
georadius key longitude latitude 100 m
Any pros and cons?

MongoDB Atlas - Geolocation

So I am using Mongodb Compass and trying to test out Geolocation while MongoDB website is very good at terminal coding I need to be able to do the same thing as below in mongoDB atlas - just to make sure the code works.
I plan on using Mongoose as the driver in my react app to fetch the data, however I just thought before I go to far I should see if this is really possible (now I understand Mongodb Compass is different to Mongoose and the query maybe a little different.)
db.campaign.find({ location:
{ $geoWithin:
{ $centerSphere: [ [ 115.880321, 31.9256201], 5 / 3963.2 ] } } })
The collection looks like this
{"_id":{"$oid":"55cba2476c522cafdb053add"},"location":{"coordinates":[115.880321,-31.9256201],"type":"Point"},"name":"DRN1"}
It currently throws an error an does not let me even attempt to search this.
Query
{ location: { $nearSphere: { $geometry: { type: "Point", coordinates: [ 115.880321, -31.9256201 ] }, $maxDistance: 5 * 1609.34 } } }

How to get unique mongodb?

If I have documents like this:
{firstname:"Jordan", lastname:"Snyder", age:6, homelocation:[<longitude, latitude>]}
In the mongo shell, how do I all the "distinct" firstname's across matching documents of people who live near a specific point (say 1 mile)? I see mongo has a distinct db.collection.distinct(field, query), but all the samples I see for finding anything "near" or "geowithin" (using homelocation field in my case) is using db.collection.find. I don't want all documents, I just want the distinct list of firstnames.
The query parameter of distinct uses the same format as the query selector parameter of find. So assuming a 2dsphere index on homelocation you can do something like:
db.test.distinct('firstname', {
homelocation: {
$near: {
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$maxDistance: 1600 // In meters
}
}
})

Mongodb geospatial query performance

I am writing a nodejs app involving geocodes ( ~50million places) stored in mongodb (using mongo native driver). For development I am testing with sample data (airports of the world ~45k locations http://www.ourairports.com/data/airports.csv ). I am following GeoJSON format and building 2dsphere index on location field(named "geometry"). Then I am doing a $geowithin query to find locations.
Evrything works as expected. The issue is that when I am querying for a polygon containing US, the query is taking around 4-6sec for returning 22845 locations.
db. airports.ensureIndex({ geometry: '2dsphere' })
db.airports.find({ geometry: { '$geoWithin': { '$geometry': { type: 'Polygon', coordinates: [ [ [ -127.32917843921399, 75.11297289119061 ], [ -71.32126356078601, 75.11297289119061 ], [ -71.32126356078601, 12.305525108809391 ], [ -127.32917843921399, 12.305525108809391 ], [ -127.32917843921399, 75.11297289119061 ] ] ] } } } })
I am sure that this cant be correct. This way I cant imagine response times with 50million points.
Would appreciate if someone can point me in direction to improve the performance of this.