2015-11-27 18 views
6

nel mio progetto ci sono due requisitiMongo DB - geospaziali query con la posizione e il raggio di campo (field confronto interno)

primo: Ho la collezione sotto

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

Per questo ho per trovare tutti la posizione che corrispondono alla mia posizione con un raggio particolare in modo da utilizzare questa query:

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

La query precedente funziona correttamente.

secondo: Ho la collezione come below.here sto affrontando problema.

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

Qui ho il raggio della mia raccolta. L'input dell'utente è loc solo

var myLoc = [ 14.67, 56.78 ]; 

Ora devo trovare tutti i documenti che corrispondono alla mia posizione con la loro posizione e il loro raggio.

query che ho provato come

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" 

poi

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


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

poi

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" ] } }

Sono abbastanza nuovo per MongoDB query..Even vostro piccolo suggerimento vi aiuteranno io in un modo fantastico Per favore, fammi sapere come ottenere ciò. vostro aiuto è molto è apprezzato

risposta

1

È possibile utilizzare l'operatore di $where:

model.find({'$where': function() { 
    var myLoc = [ 14.67, 56.78 ]; 
    return { 'loc': { 
     '$geoWithin': { "$centerSphere": [ myLoc, this.Radius ] } } 
    }} 
}) 
Problemi correlati