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 } } }
Related
I started to work with MongoDB API, beacause we are using Azure Cosmos DB.
Tried examples using $near in MongoDB with the basic structure {key:"A": localtion:{type:"Point", coordinates:[1,2]}}, and works well. The ploblem is when i need to use an array of locations.
I'm triying to execute this query without result. What i'm doing bad?
db.places.insert( {
id:1,
name: "AAAAAAAAAAA",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7191 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:2,
name: "BBBBBBBBBBB",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7193 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:3,
name: "CCCCCCCCCCCCC",
locals:[
{
location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
}
],
category: "Parks"
} );
//Create index
db.places.createIndex({ "locals.location" : "2dsphere" })
//Query without result
db.places.find(
{
"locals.location":
{ $near:
{
$geometry: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
The places has many locals, that's why I can have many objects inside.
I hope some one can help me.
Cheers.
Based on my test with your sample data, also got empty result as same as you. Per my knowledge, CosmosDB just supports a subset of the MongoDB API and translates requests into the CosmosDB SQL equivalent. CosmosDB has some different behaviours and results. But the onus is on CosmosDB to improve their emulation of MongoDB.
Certainly, you could add feedback here to submit your requirements or consider using MongoDB Atlas on Azure if you'd like full MongoDB feature support.
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
Hello MongoDB documentation specifies that there are several ways to retrieve documents near a geographical position :
using "queries" (in find queries for instance) :
https://docs.mongodb.com/v3.0/reference/operator/query-geospatial/
or using "commands" :
https://docs.mongodb.com/manual/reference/command/nav-geospatial/
I don't understand the difference between commands and operator/queries ? These commands seem to do exactly the same thing as their query counterparts ?
PS : I use scala reactivemongo connector in my application.
I remembered reading it from mongodb definitive guide 2nd edition (mongo 2.6), however this book only covers mongo2.6.
query command cover several tasks like CRUD, drop database. While database Command cover everything else, including administrative tasks, cloning database, etc. (this book uses mongodb v2.6, I am sure in mongodbV3.2 queryCommand has more functions that can cover some adminitrative tasks)
query in mongoshell returned a cursor, while database command returned an document that always has "ok" status, and one or more information.
example querying geolocation with database Command
db.runCommand( {
geoNear: <collection> ,
near: { type: "Point" , coordinates: [ <coordinates> ] } ,
spherical: true,
...
} )
example of querying database with query command
db.places.find(
{
location:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
I currently have a query on MongoDB which queries for devices located within a certain distance of a location. It's been working perfectly fine for months now. When I upgraded from MongoDB 2.4.10 to 2.6.3, the query no longer works and it returns no results.
{
"lastLocation": {
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [ -122.195 , 37.423]
}
},
"$maxDistance": 10000
}
}
After playing around with it, it seems that without $maxDistance, the query works. However, I can't get $maxDistance to work at all.
I have two databases side by side with the exact same data, one with each version of MongoDB, 2.4 returns data, 2.6 does not (so it's not because there aren't devices within that distance).
Is there a known issue with maxDistance, or was there a change in units or how it works?
Index is as follows:
{
v: 1,
name: "lastLocation_2dsphere",
key: {
lastLocation: "2dsphere"
},
ns: "s-dev.devices",
background: true,
safe: true
}
Sample entry:
"lastLocation": [-122.19888, 37.42316]
Yes there have been a few changes in 2.6, kind of hidden in the documentation. In your case, in version 2.6 the $maxDistance needs to be inside of the $near document, not outside:
$maxDistance ChangesĀ¶
Description
For $near queries on GeoJSON data, if the queries specify a $maxDistance, $maxDistance must be inside of the $near document. In
previous version, $maxDistance could be either inside or outside the
$near document.
$maxDistance must be a positive value.
Solution
Update any existing $near queries on GeoJSON data that currently have the $maxDistance outside the $near document
Update any existing queries where $maxDistance is a negative value.
http://docs.mongodb.org/manual/release-notes/2.6-compatibility/
So in your case the query needs to look like this:
{
"lastLocation": {
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [ -122.195 , 37.423]
},
"$maxDistance": 10000
}
}
}
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.