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.
Related
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 } } }
I am using a Mongo pipeline within an aggregation lookup on 2 collections, Locations and Places.
I am trying to return all the places which these locations are near.
The error I get is 'MongoError: 'near' field must be point'
I believe this is because I am trying to use the $point variable in the pipeline from the let in the lookup and I am doing something wrong here. All the answers I see on here have static coordinates but I want to use the ones from the lookup.
This is the code I have:
return await this.placeModel.aggregate([{
$lookup : {
from : "locations",
let : {point : "location.coordinates"},
pipeline: [ {
$geoNear: {
distanceField: "distance",
near: { type: "Point", coordinates: "$point" },
maxDistance: 20,
spherical: true
}
}],
as : "places"
}
}]);
}
I have a mongoose Place model and Location model. Each model has a GeoJson point that looks like this:
location: {
type: { type: String },
coordinates: []
},
How do I reference the point properly if at all possible.
I have this structure in my collection
{
"categorie" : "Introduction",
"quart" : "jour",
"pdq" : 23,
"x" : 302375.197993,
"y" : 5046522.11601,
"lat" : 45.5586064034326,
"long" : -73.5310596776929,
"date" : ISODate("2015-01-01T00:00:00Z"),
}
I have latitude=42.5232886&longitude=-71.5923142 in query parameters.
I need to find all documents which are located at less than 3KM from the a coordinate point passed in parameter.
I am using MongoDB 3.6
Actually we don't need Haversine formula in Mongodb.Here I have done with mongoose. We need to create a schema that contain type and coordinates. You can see more details in https://mongoosejs.com/docs/geojson.html
So it's has another problem with mongoose version. Mongoose v6.3.0
worked for me. When you will use countDocuments with the query, it can
be generate error but count function not generating any error. I know
count deprecated, it shouldn't be use but I haven't find better
solution. If anyone find solution for count, let me know. Also you can visit https://github.com/Automattic/mongoose/issues/6981
const schema = new mongoose.Schema(
{
location: {
type: {
type: String,
enum: ["Point"],
},
coordinates: {
type: [Number],
index: "2dsphere",
},
},
},
{ timestamps: true }
);
const MyModel = mongoose.model("rent", schema);
The query will be
const result = await MyModel.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [Number(filters.longitude), Number(filters.latitude)],
},
$maxDistance: filters.maxRadius * 1000,
$minDistance: filters.minRadius * 1000,
},
},
})
I am trying to query for data points within a given lat/lng range. Can you reference the elements of an object in a mongoose query like I have done ('location.lat') and ('location.long')? If so, I am not getting any data back from this query. I was querying for all data points, and that was working just fine - now I am simply trying to refine the query to only the points in a given range.
EDIT
Querying for points that have a new format (see the updated schema below):
var range = {"topLeft":[-113.51526849999999,53.24204911518776],"bottomRight":[-131.0933935,41.397215826886736],"topRight":[-131.0933935,53.24204911518776],"bottomLeft":[-113.51526849999999,41.397215826886736]};
db.datapoints.find({
geo: {
$geoWithin : {
$geometry: {
type: "Polygon",
coordinates: [
[
range.topLeft, range.topRight, range.bottomRight, range.bottomLeft
]
]
}
}
}
})
but I am getting:
error: {
"$err" : "Malformed geo query: { $geoWithin: { $geometry: { type: \"Polygon\", coordinates: [ [ [ -113.5152685, 53.24204911518776 ], [ -131.0933935, 53.24204911518776 ], [ -131.0933935, 41.39721582688674 ], [ -113.5152685, 41.39721582688674 ] ] ] } } }",
"code" : 16677
}
NOTE: the range parameter looks like this:
UPDATED SCHEMA
var mongoose = require('mongoose');
var dataPointSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
geo: {type: [Number], index: '2d'},
...
timestamp: {type: Date, default: Date.now}
...
});
As seen here:
MongoDB: "Malformed geo query" with $geoIntersect on a polygon
You must close the polygon by making the first and last points the same.
I'm trying to make use of some geolocation functionality in mongodb. Using a find query with $near doesn't seem to work!
I currently have this object in my database:
{
"Username": "Deano",
"_id": {
"$oid": "533f0b722ad3a8d39b6213c3"
},
"location": {
"type": "Point",
"coordinates": [
51.50998,
-0.1337
]
}
}
I have the following index set up as well:
{
"v": 1,
"key": {
"location": "2dsphere"
},
"ns": "heroku_app23672911.catchmerequests",
"name": "location_2dsphere",
"background": true
}
When I run this query:
db.collectionname.find({ "location" : { $near : [50.0 , -0.1330] , $maxDistance : 10000 }})
I get this error:
error: {
"$err" : "can't parse query (2dsphere): { $near: [ 50.0, -0.133 ], $maxDistance: 10000.0 }",
"code" : 16535
}
Does anyone know where I'm going wrong? Any help would be much appreciated!
It seems you need to use the GeoJSON format if your data is in GeoJSON format too, as yours is. If you use:
db.collectionname.find({
"location": {
$near: {
$geometry:
{ type: "Point", coordinates: [50.0, -0.1330] }, $maxDistance: 500
}
}
})
it should work. I could replicate your error using GeoJSON storage format for the field, but what the docs call legacy points in the query expression. I think the docs are a bit unclear in that they suggest you can use both GeoJSON and legacy coordinates with a 2dsphere index 2dsphere
I am using 2.4.10, for what it is worth, as there were some big changes to spatial in the 2.4 release.
This isn't exactly a solution as I never got the above working, but using geoNear I managed to get what I wanted.
db.runCommand( { geoNear : 'catchmerequests', near:
{ type: 'Point', coordinates : [50, 50] }, spherical : true } );
If anyone can find out why the original $near attempt failed that would still be appreciated, but I'm posting this for anyone else who else who is looking for a working alternative.