Mongo DB - Geospatial Queries with location and field radius(Internal field comparision)
Asked Answered
H

2

6

In My project there are two requirements

First One : I have the collection below

{
   "name": "James",
   "loc" : [ 12.9000, 14.6733]
},
{
   "name": "James",
   "loc" : [ 54.9000, 78.6733]
}

For this I have to find all the location which match my location with a particular radius so I use this query:

var myLoc     = [ 13.5,  67.5 ];
var myRadius  = 150;
model.find({
        loc : {
            $geoWithin : {
                $centerSphere : [ myLoc, myRadius ]
            }
        }
    }
).exec()

The above query is working fine.

Second One : I have the collection like below.here i am facing problem.

{
   "name"   : "James",
   "loc"    : [ 12.9000, 14.6733],
   "radius" : 115
},
{
   "name"   : "James",
   "loc"    : [ 54.9000, 78.6733],
   "Radius" : 276
}

Here i have the radius in my collection itself. The user input is loc only

var myLoc = [ 14.67, 56.78 ];

Now I have to find all the documents which match my location with their location and their radius.

I have tried queries like

model
.where('loc').within({ center: myLoc, radius: 'this.Radius', unique: true, spherical: true })
.exec()

Error : The err is CastError: Cast to number failed for value "this.Radius" at path "undefined"

then

model
    .find(
        {
            $where: 
                {
                    Location : { 
                        $geoWithin : 
                            { $centerSphere : [myLoc, '$Radius'] }
                        }
                }
        }
    )
    .exec()


Error : Error: Must have a string or function for $where

then

model
    .aggregate( 
        { 
            $match : {
                loc : {
                    $geoWithin : {
                        $centerSphere : [ myLoc, "$Radius" ]
                    }
                }   
            }                                       
        }
     )
    .exec(  )

Error : The err is MongoError: exception: bad query: BadValue bad geo query: { $geoWithin: { $centerSphere: [ [ -1.965373600000021, 52.4744464 ], "$Radius" ] } }

I am quite new to mongodb query..Even your little suggestion will help me in a great way. Please give me the insight on how to achieve this. your help is greatly appreciated.Thanks

Hierarchize answered 27/11, 2015 at 9:12 Comment(1)
Hello I have the same problem for the same use case, how did you end up solving it? The answer below is deprecatedRaffarty
M
1

You can use the $where operator:

model.find({'$where': function() {
    var myLoc = [ 14.67, 56.78 ];
    return { 'loc': { 
        '$geoWithin': { "$centerSphere": [ myLoc, this.Radius ] } }
    }}
})
Molarity answered 27/11, 2015 at 9:25 Comment(1)
Starting in MongoDB 4.4, $where no longer supports the deprecated BSON type JavaScript code with scope (BSON type 15). The $where operator only supports BSON type String (BSON type 2) or BSON type JavaScript (BSON type 13). The use of BSON type JavaScript with scope for $where has been deprecated since MongoDB 4.2.1.Solifidian
S
0

Sample collection data

{ 
"business": "Store",
"geo": {
    "name": "StoreName",
    "coordinates": [80.628913, 13.622177],
    "type": "Point"
},
"address": "Some Address",
"city": "Kolkata",
"pincode": "700001",
"startTime": "10:00:00 AM",
"closingTime": "7:00:00 PM",
"closedOn": "Closed on Sunday",}

Create 2dSphear index on db.collectionName.createIndex({geo:'2dsphere'}) Now use the $geoNear aggregation query to smoothly fetch record.

db.collectionName.aggregate([
       {
         $geoNear: {
            near: { type: "Point", coordinates: [  80.3355,13.6701  ] }, // Mandatory Param.
            distanceField: "dist.calculated",          // It will return in the document how much distence (meter) is far feom center
            maxDistance: 2000,                         // Fetch 2KM radius store, Distance in meter from the center 
            spherical: true,                           // calculates distances using spherical geometry.
            query:{pincode:"700001"}                  // Specific Query parameter on other field present in the collection
         }
       }
    ]).pretty()

This is a working code and tested and verified.

Solifidian answered 21/4, 2021 at 6:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.