Per title – I am using the official mongodb driver and I am looking to get all POIs within the given bounding box.
So far I have:
MongoCollection<BsonDocument> collection = _MongoDatabase.GetCollection("pois");
BsonArray lowerLeftDoc = new BsonArray(new[] { lowerLeft.Lon, lowerLeft.Lat});
BsonArray upperRightDoc = new BsonArray(new[] { upperRight.Lon, upperRight.Lat});
BsonDocument locDoc = new BsonDocument
{
{ "$within", new BsonArray(new[] { lowerLeftDoc, upperRightDoc})}
};
BsonDocument queryDoc = new BsonDocument { { "loc", locDoc }};
IList<TrafficUpdate> updates = new List<TrafficUpdate>();
var results = collection.Find(new QueryDocument(queryDoc)).SetLimit(limit);
foreach (BsonDocument t in results)
{
}
Unfortunatelly this doesnt work. I get:
QueryFailure flag was unknown $within type: 0 (response was { “$err” :
“unknown $within type: 0”, “code” : 13058 }).
The problem in your code is that you didnt specify which geo operation you wanted to use. You only specified
$withinbut missedwhere. you must specify$withinalong with$box(bounding box) ,$polygon,$centeror$centerSphere/$nearSphere.This is the correct mongo syntax to run
$boxqueriesi am not sure about the c# mongodb syntax. But if you include ‘$box’, it ll work