So I have a query I am running at the moment which allows me to allow a business to show up if a person is within 10km.
However I want to add the ability to also show people if the user is inside a polygon.
I want to be able to do both queries at the same time in one query.
db.geom.insert({
"polygons": {
"type":"Polygon",
"coordinates": [[
[ 17.60083012593064, 78.18557739257812],
[ 17.16834652544664, 78.19381713867188],
[ 17.17490690610013, 78.739013671875],
[ 17.613919673106714, 78.73489379882812],
[ 17.60083012593064, 78.18557739257812]
]]
}
});
To do the polygon query I can do this.
db.geom.find({
polygons: {
$geoIntersects: {
$geometry: {
"type": "Point",
"coordinates": [17.3734, 78.4738]
}
}
}
});
I currently have a query that does this.
advertquery = { location:{ $geoWithin:{ $centerSphere: [ [ parseFloat(docs.location.coordinates[0]), parseFloat(docs.location.coordinates[1])], 10 / 6378.1] } } };
//START COUNT
var adquery = GEMS.countDocuments(advertquery).exec(function(err, count){
adcount = count
});
To get these two queries to work I have had to write this code.
advertquery = {
$or: [
{location:{
$geoIntersects: {
$geometry: {
"type": "Point",
"coordinates": [17.3734, 78.4738]
}
}
}
},
{ location:{
$geoWithin:{
$centerSphere: [ [ 17.3734, 78.4738], 10 / 6378.1] } } }
]
}
//START ADCOUNT
var adquery = GEMS.countDocuments(advertquery).exec(function(err, count){
if(err){console.log(err)}
console.error("TOTAL COUNTED : "+count);
adcount = count
});
However the countDocuments is not returning the count, just 0.
But when I run this is compass I get 1 result.
From the mongodb / mongoose documentation, it states countDocuments() does not support certain operators, that includes $geoWithin that you have used in the above query but can be replaced as mentioned in the docs.
Related
I'm using MongoDB to store about 1 million documents representing regions.
Each document contains a coordinates record in the following format
"coordinates" : {
"longitude" : -77.02687,
"latitude" : 38.888565
}
Given a set of coordinates { x, y }, what query should I run to find the region ( document ) that is closest to it?
Based on the MongoDB geospatial-queries documentation the answer is quite simple.
In order to query for locations near a region you should follow these steps
Step 1
Create an index on the location field
db.places.createIndex( { location: "2dsphere" } )
Step 2
Find regions close to { -73.9667, 40.78 } ordered by closest locations
db.places.aggregate( [
{
$geoNear: {
near: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
spherical: true,
query: { category: "Parks" },
distanceField: "calcDistance"
}
}
] )
I am not sure if there is a way to do this. I need to have a circle in mongodb and run a query against that with a box using $box to see if these two shapes overlap or not. However, Geojson does not support circles. What would be the best way to get this done?
The circle is stored like this:
places = {
...
"location": {
"type": "Point",
"coordinates": [
-79.390756,
43.706685
]
},
"radius": 100
}
I have two specific problems:
The first issue is that maxDistance is stored in the same object as the Geojson object and cannot be used in a $near query with $maxDistance; it only takes a number.
I do a partial postal code/ zip code search on Google Geocoding Api which returns a box with two corner coordinates like this:
"geometry": {
"bounds": {
"northeast": {
"lat": 43.710565,
"lng": -79.37363479999999
},
"southwest": {
"lat": 43.690848,
"lng": -79.40025399999999
}
}
As far as I know,I cannot use $box as it only works with $geoWithin.
Edit 1:
My initial plan with the circle and the box changed mainly because I did not find a suitable and efficient solution to this problem. Instead of checking if the circle overlaps with the box, now I check if a Geojson point is inside the circle as follows:
db.places.aggregate([
{"$geoNear": {near: { type: "Point", coordinates: [ -80.459293, 40.713640] },
distanceField: "dist.calculated", maxDistance: 100000,
key: 'myLocation', query: { 'SomeField': "..." }, spherical: true}},
{ "$match" : {$expr:{ $lte:['$dist.calculated', 'radius']}}}])
The problem here is that I d have to run a query within 100 KM first and then in another stage of the aggregation check the distance.
Is there a more efficient way to implement this? Thanks.
You can store a circle as point and radius. And you can use a $near query with a point and $maxDistance in meters which is the radius of the circle. See MongoDB Documentation.
Query to find all location, geometry field of the collection, at a certain distance from a point.
db.places.find(
{
location:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$maxDistance: 5000
}
}
}
)
Query to find if a given geometry (point, polygon(rect too)) in a query intersects with a geometry of a field in the collection.
//find quests bots that matches the users location
await Collection.find({ geometry:
{ $geoIntersects:
{
{
type: "Point",
coordinates: [
-73.99460599999999,
40.7347229
]
}
}
}
});
I have a dataset of ~400k objects in the format:
{
"trip": {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[
-73.9615,
40.6823
],
[
-73.9704,
40.7849
]
]
},
"properties": {
......
}
}
}
I tried making a 2dsphere index on mLab like so:
{"trip.geometry" : "2dsphere"}
Which I assume just calls:
db.collection.createIndex( {"trip.geometry" : "2dsphere"} )
When I try to do a $geoWithin query like so (about 500 hits):
db.collection.find(
{
"trip.geometry": {
$geoWithin: {
$geometry: {
type : "Polygon" ,
coordinates: [
[
[-74.0345,40.7267],
[-73.9824,40.7174],
[-73.9934,40.7105],
[-74.0345,40.7267]
]
]
}
}
}}
)
I noticed it was very slow, ~2 seconds. I then tried deleting the index entirely, and the time increase was very slight. ~0.5 seconds. Is it possible that this query is not using the index the I had set? I've included the explain() here.
By my interpretation, the winning plan first fetches all the data based on a simple filter, then uses the 2dindex. Shouldn't it start out using the 2dindex, given that the lat and lon data aren't indexed directly?
mongo 2.6
I have some amount of stored polygons. And I have a point. I what to know if this point fits any of stored polygons
document example
{ ..., "polygons" : [ [ 17.60083012593064, 78.18557739257812 ], [ 17.16834652544664, 78.19381713867188 ], [ 17.17490690610013, 78.739013671875 ], [ 17.613919673106714, 78.73489379882812 ] ], ... }
There is nearly the same question already Mongodb : Check if a point is inside a stored polygon.
But it is not working for me - this query has to give at least one result(the one in example) - but it does not.
db.areas.find( { polygons : { $geoIntersects : { $geometry : {type:"Point",coordinates:[17.3734, 78.4738]} } } } )
Actually if I chose a point on a border of any polygon - it does.
$geoWithin method has to do the work as mondodb documentation says.
but any of these queries do not work
db.areas.find( { polygons : { $geoWithin : { $geometry : {type:"Point",coordinates:[17.3734, 78.4738]} } } } ) - not supported with provided geometry
db.tradeareas.find( { polygons : { $geoWithin : { $geometry : {type:"Polygon",coordinates: inside_polygon} } } } ) - BadValue bad geo query
It seems I miss something but cant understand what and where.
I would be grateful for help.
It seems to be to do with the order. If you are using $geoWithin and you are trying to find points inside a polygon, the thing that is within is the field you are searching on. However, $geoIntersects works in either direction, so you can search for points inside polygons, or polygons containing points, eg:
db.geom.insert({
"polygons": {
"type":"Polygon",
"coordinates": [[
[ 17.60083012593064, 78.18557739257812],
[ 17.16834652544664, 78.19381713867188],
[ 17.17490690610013, 78.739013671875],
[ 17.613919673106714, 78.73489379882812],
[ 17.60083012593064, 78.18557739257812]
]]
}
});
db.geom.find({
polygons: {
$geoIntersects: {
$geometry: {
"type": "Point",
"coordinates": [17.3734, 78.4738]
}
}
}
});
Also, note that, you need to repeat the first point of the polygon at the end. If you remove the final pair, you will get a $err:
Can't canonicalize query: BadValue bad geo query" error.
It seems that MongoDB allows you to insert invalid geometries and only complains when you try and add a 2dsphere index or do an intersects/within/near query, which, I suppose is reasonable, as GeoJSON can be valid JSON without being a valid geometry.
Thanks for John Powell here is C# driver version of the same query.
var geometry = new BsonDocument
{
{ "type", "Point" },
{ "coordinates",
new BsonArray(new double[]{ Longitude,
Latitude} ) }
};
var geometryOperator = new BsonDocument { { "$geometry", geometry } };
var geoIntersectsOperator = new BsonDocument { { "$geoIntersects", geometryOperator } };
var findField = new BsonDocument { { "geometry", geoIntersectsOperator } };
var results = MyCollection.Find(findField).ToList();
In Java it Can be done this way
#Autowired
private MongoOperations mongoOpertions;
public void pointIntersect(GeoJsonPoint gp){
Query query = new Query();
query.addCriteria(Criteria.where("geometry").intersects(gp));
List<ResultDtoType> result = mongoOpertions.find(query, ResultDtoType.class);
//perform any action with result
}
I would like to have a MongoDB collection and each document contains a geospatial polygon defined by latitude/longitude points (in GeoJSON). Then, I would like to take any given longitude/latitude point and check if it resides within any of the MongoDB polygons defined in the documents. Hypothetically, this is what the documents would look like.
{
"type" : "congressional",
"points" : [
{ "coords" : [
-141.0205,
70.0187 ]
},
...
{ "coords" : [
-141.0205,
70.0187 ]
}
]
}
Or maybe like so:
{ loc :
{ type : "Polygon" ,
coordinates : [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]
} }
And then I would query it, hypothetically, like so (most likely with Mongo's $geoWithin):
db.places.find( { loc : { $geoWithin : { $geometry : "EACH DOCUMENT IN COLLECTION"} } } )
Is geofencing, or something similar, possible to do with the current MongoDB feature-set? If so, how would it be done?
I believe you would have to first find every document from the collection., and then make a "$geoWithin" query for every document, passing in the Polygon to test against for each case.
Depending on the number of documents in your collection, that may or may not provide sufficient performance.
MongoDB has full support for geofencing or finding documents whose geometry intersects with a given geometry (point, polygon). The query below is an example. geometry of Collection is the field containing the geometry.
Document Example:
{ "geometry": {
"type": "Polygon",
"coordinates": [
[
[
-74.001487,
40.692346
],
[
-74.001755,
40.692057
],
[
-74.000977,
40.691951
],
[
-74.000827,
40.692297
],
[
-74.001487,
40.692346
]
]
]
}
}
JS Query:
//find quests bots that matches the users location
await Collection.find({ geometry:
{ $geoIntersects:
{
{
type: "Point",
coordinates: [
-73.99460599999999,
40.7347229
]
}
}
}
});