Reputation: 14750
Let's say user finds documents with in Wellington New Zealand and provide coordinates of Wellington city: lat: -41.2864603, lng: 174.77623600000004
.
But if there are no documents within this city I need to search documents within nearest cities within New Zealand country.
How could I find documents within country?
Upvotes: 0
Views: 1139
Reputation: 151112
Most of the "heavy lifting" if this is really done by the general geospatial query itself, so long as you are using operations like $near
or ideally $nearSphere
with a 2dsphere index to allow for the curvature of the earth. Such queries basically find the "nearest" points to the queried "point" or GeoJSON object supplied and sort the results in the response that way.
Original implementations with the geoNear command had a default "limit" of 100 documents in the response and the ability to set the limit of documents in response. This is still generally valid as you would usually not want too many responses from such a query as there is a point where things are not "near" at all.
But the general case is that if you want to have additional query parameters in your logic then you can just add them to the existing query:
db.collection.find({
"$nearSphere": {
"$geometry": {
"type": "Point",
"coordinates": [ 174.77623600000004, -41.2864603 ]
}
},
"$or": [
{ "city": "Wellington" },
{ "country": "New Zealand" }
]
})
The logic there is that the queried objects must be "near" the queried location data and also either have data matching the "city" of "Wellington" $or
matching "country" of "New Zealand".
You could alternately represent a complete "or" condition where the object did not necessarily match the geolocation condition, as long as the other query parameters matched something:
db.collection.find({
"$or": [
{ "$nearSphere": {
"$geometry": {
"type": "Point",
"coordinates": [ 174.77623600000004, -41.2864603 ]
}
}},
{ "city": "Wellington" },
{ "country": "New Zealand" }
]
})
That is of course if you "really want to" as it should not be needed generally speaking as the selected objects will already be returned ordered by the "nearest" results and there are other ways to "bound" those results to within a certain "distance". But if you really want to specify additional "bounds" then you can.
Of course when issuing either a $near
or $nearSphere
query or other variation, a "2d" or "2dSphere" index must be in place. The other constraint is that the data must either be represented in your document in either the form of legacy coordinate pairs or in GeoJSON format. In all cases the representation is <longitude>
, <latitude>
.
So just having fields present in the document to represent "longitude" and "latitude" is not enough in itself and you need a supported storage format along with the "index" to use the operators as shown.
The other data in the conditions can be contained in other indexes, following the guidelines for query selection with and $or
condition, or within a compound index as along as that data meets the rules for complexity of what can be combined. It isn't necessary for additional data fields to be indexed, but it is usually the most optimal way to query with addition query parameters.
Upvotes: 1