I am trying to get all data within viewport
"viewport": {
"northeast": {
"lat": -33.8652709197085,
"lng": 151.1972016802915
},
"southwest": {
"lat": -33.8679688802915,
"lng": 151.1945037197085
}
}
i have data in collection with data like
{data:"Block 1", location: { "coordinates" : [ 77.58556519999999, 12.943470099999999 ], "type" : "Point" }}
i tried it query on mongodb as
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [27, 83.22].55
},
$maxDistance: 3000
}
}
but i want data within rectange frame how can i acgheve that ?
Related
I have the following document in my MongoDB collection, which I would like to be able to do a query that check if point that is provided by the user is inside a bbox array that is stored in the collection.
{
"type": "Feature",
"properties": {
"place_id": 298104298,
"osm_type": "relation",
"osm_id": 80500,
"display_name": "Australia",
"place_rank": 4,
"category": "boundary",
"type": "administrative",
"importance": 0.8521350639151115,
"address": {
"country": "Australia",
"country_code": "au"
}
},
"bbox": [
72.2461932,
-55.3228175,
168.2261259,
-9.0880125
]
}
What I would like to do is a geoIntersect or geoWithin query.
for example:
[{
$match: {
bbox: {
$nearSphere: {
$geometry: {
type: 'Point',
coordinates: [
-73.9667,
40.78
]
}
}
}
}
}]
I have also tried
[{
$project: {
geometry: 0
}
}, {
$match: {
bbox: {
$geoWithin: {
$box: [
[
-73.9667,
40.78
],
[
40.78,
-73.9667
]
]
}
}
}
}]
However that did return results but wrong results the geo location should return NULL as the location is Antarctic Ice shield, Antarctica - (-73.9667,40.78)
I want to find out which Polygon from a collection, contains the most points from another collection.
I’m using one collection with restaurant data (points) and one with the neighborhood data (polygons)
Both collections are provided by mongodb:
https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/neighborhoods.json
https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/restaurants.json
Neighborhood document:
{
"_id": {
"$oid": "55cb9c666c522cafdb053a1a"
},
"geometry": {
"coordinates": [
[
[
-73.9443878859649,
40.70042452378256
],
[
-73.94424286147482,
40.69969927964773
],
[
-73.94193078816193,
40.70072523469547
],…
]
],
"type": "Polygon"
},
"name": "Bedford"
}
Restaurant document
{
"_id" : { "$oid" : "55cba2476c522cafdb053add" },
"location" : {"coordinates":[-73.856077,40.848447] , "type":"Point" },
"name" : "Morris Park Bake Shop"
}
Here an example to find out all restaurants within a single district:
Select one neighborhood (polygon) by a given point ($geoIntersects)
var neighborhood = db.neighborhoods.findOne(
{
geometry:
{
$geoIntersects:
{
$geometry: { type: "Point", coordinates: [ -73.93414657, 40.82302903 ] }
}
}
}
)
Find out how many restaurants are in this neighborhood
db.restaurants.find( { location: { $geoWithin: { $geometry: neighborhood.geometry } } } ).count()
My question:
Which neighborhood contains the most restaurants?
this is the query I would like to perform:
db.cells.find(
{
loc: {
$near: {
$geometry: {
type : "Point",
coordinates : [ 31.0, 31.0 ]
},
$minDistance: 0,
$maxDistance: this.range
}
}
}
)
The problem is with this.range. I would like to use the field range of cells to set the max distance. This parameter can vary or maxDistance must be a fixed value for every document I try to match?
One of the workarounds to your issue is computing a distance field using $geoNear in aggregation pipeline. Then, use the computed distance field to compare with the max Distance you want(i.e. this.range in your case).
db.cells.aggregate([
{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [
31.0,
31.0
]
},
"distanceField": "dist"
}
},
{
$match: {
$expr: {
lte: [
"$dist",
"$range"
]
}
}
}
])
Mongo Playground
db.places.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] },
distanceField: "dist.calculated",
maxDistance: 2,
query: { type: "public" },
includeLocs: "dist.location",
num: 5,
spherical: true
}
}
])
I am Using aggregate method in mongodb.and this is working for me but I need projection on the document for only return some field to show. So how can I achieve projection in this method
Even OP's problem is solved in the comments I decided to add an detailed answer as it will help some one else.
As #hecnabae said you have to append $project stage to your query to retrieve data.
Here is an example:
db.getCollection("places").aggregate(
[
{
"$geoNear": {
"near": { "type": "Point", "coordinates": [ -73.99279 , 40.719296 ] },
"distanceField: "dist.calculated",
"maxDistance": 2,
"query": { "type": "public" },
"includeLocs": "dist.location",
"num": 5,
"spherical": true
}
},
{
"$project" : {
"Name" : 1.0,
"Distance" : "$dist.calculated",
"Location": "$dist.location"
}
}
],
{
"allowDiskUse" : false
}
);
This will return you an list of data as follows:
{
"_id" : ObjectId("5e3207128cbfe302dcbb1234"),
"Name" : "Your Place Name",
"Distance" : 17.98627020911569,
"Location" : [
58.1230154,
23.1232555
]
}
I have user collection:
{
"_id": { "$oid" : "514C438232F5699004000014" },
"gender": 1,
"loc": {
"coordinates": [
0.777084,
0.701690
],
"type": "Point"
},
"name": "H1",
"radius": 1
},
{
"_id": { "$oid" : "514C438232F5699004000014" },
"gender": 1,
"loc": {
"coordinates": [
0.677084,
0.701690
],
"type": "Point"
},
"name": "H2",
"radius": 0.4
}
db.user.ensureIndex( { loc : "2dsphere" } )
I need to write query and use radius property from collection's row ( "radius": 1 ) in find query like this:
db.user.find( { loc: { $geoWithin :{ $centerSphere : [ [0.7, 0.7 ] , radius ]} } } )
But mongo returns:
JavaScript execution failed: ReferenceError: radius is not defined
I have tried db.user.find( { loc: { $geoWithin :{ $centerSphere : [ [0.7, 0.7 ] , this.radius ]} } } )
I think you have to do a two way query. First fetch the radius of a given user, then search for all location within this radius.