2013-05-10 63 views
2

我有用戶採集:MongoDB:如何在查找查詢中使用屬性?

{ 
    "_id": { "$oid" : "514C438232F5699004000014" }, 
    "gender": 1, 
    "loc": { 
    "coordinates": [ 
     0.777084, 
     0.701690 
    ], 
    "type": "Point" 
    }, 
    "name": "H1", 
    "radius": 1 
}, 

{ 
    "_id": { "$oid" : "514C438232F5699004000014" }, 
    "gender": 1, 
    "loc": { 
    "coordinates": [ 
     0.677084, 
     0.701690 
    ], 
    "type": "Point" 
    }, 
    "name": "H2", 
    "radius": 0.4 
} 

db.user.ensureIndex({ loc : "2dsphere" }) 

我需要寫的查詢和收集的行使用半徑屬性(「方圓」:1)在發現這樣的查詢:

db.user.find({ loc: { $geoWithin :{ $centerSphere : [ [0.7, 0.7 ] , radius ]} } }) 

但蒙戈的回報:

JavaScript execution failed: ReferenceError: radius is not defined

我已經試過db.user.find({ loc: { $geoWithin :{ $centerSphere : [ [0.7, 0.7 ] , this.radius ]} } })

回答

0

我認爲你必須做一個雙向查詢。首先獲取給定用戶的半徑,然後搜索該半徑內的所有位置。