How to run geosaptial Query in Mongo compass UI? - mongodb

I have used Mongo Compass.. i want run following geospatial query in Mongo compass
db.restaurants.find( { location : { $geoWithin :
{ $centerSphere :
[ [ 88 , 30 ] , 10 / 3963.2 ]
} } } )
Above Query is actual query. and also i have 2 collection these are restaurants and neighborhoods..
see following image

You can also click at any point on the map, hold shift key and drag the pointer to somewhere else. It will build a query for you. So you can use it as a sample for building your own.

Try to enter only the query object into the input field, without db.restaurants.find... like so:
{ location : { $geoWithin :
{ $centerSphere :
[ [ 88 , 30 ] , 10 / 3963.2 ]
} } }

Related

mongoDB Geospatial Query $geoWithin got error 'Loop is not closed'

i am newbie for Geospatial Query of mongodb and i try search for point within polygon which have code like below :
db.properties.find({
location:{
$geoWithin : {
$geometry : {
type:"Polygon",
coordinates: [[
[104.9172382, 11.57934524],
[104.9146632, 11.5724502],
[104.92753788, 11.57976566]
]]
}
}
}
})
and my data table look like below :
i want to get result of all point is inside polygon.but i really have no clue
this error alway popup
|____/ Mongo Server error (MongoQueryException): Query failed with
error code 2 and error message 'Loop is not closed: [ [ 104.9172382,
11.57934524 ], [ 104.9146632, 11.5724502 ], [ 104.92753788, 11.57976566 ] ]' on server
thank for any idea !
i found answer that we need to start coordinate and close coordinates are the same value.
db.properties.find({
location:{
$geoWithin : {
$geometry : {
type:"Polygon",
coordinates:[
[
[ 104.9212999921292 , 11.575955591122819 ] , // start loop
[ 104.92129194550216 , 11.575198826419006 ] ,
[ 104.92298978380859 , 11.575238241297862 ] ,
[ 104.92291736416519 , 11.576023910057827 ] ,
[ 104.9212999921292 , 11.575955591122819 ] // close loop
]
]
}
}
}
})

MongoDB error while implementing $near query

Sample MongoDB Document
{
"_id":"5adedaad0bd4134kb0",
"url":"https://iscon.cdnalign.com/t51.288515/s640x640e35/10249200241120_2108192950_n.jpg?ig_cache_key=MTEYUTB1NDgxN",
"description":"#funinthesun",
"locationName":"Calamari",
"statusValue":1,
"reason":"Related to travel",
"category":["park"],
"geo": {
"index":"Point",
"coord":[29.123024,77.1999]
}
}
I wand to get document according to there distance from a particular point.
and this is the query that I am using so that documents come according to distance.
Query
var collection = db.collection('myData');
collection.ensureIndex({"geo.index":"Point"});
collection.find({
geo :{
$near : {
$geometry : {
index : "Point" ,
coord : [30.564058, 76.44762696]
},
$maxDistance : 100
}
}
}
and this is showing me this error :-
{"name":"MongoError","message":"invalid point in geo near query $geometry argument: { index: \"Point\", coord: [ 4.27326978424058, 30.4439024447627 ] } Point must be an array or object","waitedMS":0,"ok":0,"errmsg":"invalid point in geo near query $geometry argument: { index: \"Point\", coord: [ 4.27326978424058, 30.4439024447627 ] } Point must be an array or object","code":2}
If you check the $near documentation https://docs.mongodb.com/manual/reference/operator/query/near/ you will see that you have two issues here :
You need an index of type 2dsphere in order to make geospacial queries on GPS fields
A geospacial Point must have a property of name coordinates and not coor
Here is a correct version of your code that should works
var collection = db.collection('myData');
collection.ensureIndex({"geo":"2dsphere"});
collection.find({
geo :{
$near : {
$geometry : {
index : "Point" ,
coordinates : [30.564058, 76.44762696]
},
$maxDistance : 100
}
}
})
https://docs.mongodb.com/manual/reference/operator/query/near/
May be wrong here but from docs $geometry requires two parameters index and coordinates. You have coord. Maybe this is the issue. Change
coord : [30.564058, 76.44762696]
to be
coordinates : [30.564058, 76.44762696]
Also change name in DB too.
Hope this helps.

Only retrieve back select sub properties and limit how many items they contain

I have a very simple document:
{
"_id" : ObjectId("5347ff73e4b0e4fcbbb7886b"),
"userName" : "ztolley",
"firstName" : "Zac",
"lastName" : "Tolley"
"data" : {
"temperature" : [
{
"celsius" : 22,
"timestamp" : 1212140000
}
]
}
}
I want to find a way to write a query that searches for userName = 'ztolley' and only returns back the last 10 temperature readings. I've been able to say just return the data field but I couldn't find a way to say just return data.temperature (there are many different data properties).
When I tried
db.user.find({userName:'ztolley'},{data: {temperature: {$slice: -10}}})
I got unsupported projection.
I'd try using the Aggregation framework http://docs.mongodb.org/manual/aggregation/ .
Using your schema this should work:
db.user.aggregate([{$match:{"userName":"ztolley"}},{$unwind:"$data.temperature"},{$sort:{"data.temperature.timestamp":-1}},{$limit:10}, {$project:{"data.temperature":1, _id:0}}])
It returns the temperature readings for that user, in reverse sorted order by timestamp, limited to 10.
It looks you write wrong projection, try it with dot '.' notation:
db.user.find( { userName:'ztolley' },{ 'data.temperature': { $slice: -10 } } );

MongoDB How to find which polygon contains a specified Point?

I insert many polygons into MongoDB(2.4.8), and hope find the polygon a specified Point sits in. It seems a common question. But after reading all docs from google, I didn't get the result. So create this question.
e.g.
db.addr_poly.insert(
{ loc :
{ type : "Polygon" ,
coordinates : [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]
} })
db.addr_poly.insert(
{ loc :
{ type : "Polygon" ,
coordinates : [ [ [ 0 , 0 ] , [ -3 , -6 ] , [ -6 , -1 ] , [ 0 , 0 ] ] ]
} })
Now how to find the polygon which contains Point(1,1)?
Anybody can help me? Thanks a lot!
Use the $geoIntersects operator. It queries for all shapes which intersect the GeoJSON object you pass. When you pass a point to it, it should return all shapes which include that point. Keep in mind that the $geoIntersects operator only works for 2dsphere indexes, not for 2d indexes.
As #philipp said, you should use the $geoIntesects operator. I was having the hardest time trying to figure out the query. I figured I would share what it looks and it might save someone the trouble later.
db.addr_poly.find({
loc:{
$geoIntersects: {
$geometry: {
type: "Point" ,
coordinates: [1, 1]
}
}
}
});

MongoDB: geospatial query with additional conditions

{ _id : ObjectId(...),
name : "...",
addresses : [ {
context : "home" ,
loc : [ 55.5, 42.3 ]
} ,
{
context : "office",
loc : [ -74 , 44.74 ]
}
]
}
address.loc is "2d" indexed.
I want to write a query that should give me all the document that are $near a location and the context is office.
I wrote some thing like:
db.coll.find({'address.loc':{$near:[lat,lng]}, 'address.context' : "office"});
Above query doesn't give me results that wanted. It searches for location in the entire "Address" array and then searches for context in the entire array.
I would like to search for same array location and same context. I know it could be done by $elemMatch but when I try to use it, it says there is no 2d index available or 2dsphere index.
I am new to MongoDB and not sure how should I write my query.
I've tried the query and it seems to work as you intend with the $elemMatch operator. I think the problem is that you have a typo in your query where address is used instead of addresses. Your query should look like:
db.coll.find({ 'addresses.loc':{$near:[lat,lng]}, addresses: { $elemMatch: {context: "office"} } });