开发者

Mongo geo results with 10gen official c# driver

I have just downloaded the most recent official 10gen mongo c# driver (1.1.0.4184) and I am trying to do what I think is a pretty simple query to get back some results based on location:

MongoServer server = MongoServer.Create("xxxxxx"); 
MongoCredentials credentials = new MongoCredentials("user", "pw");
MongoDatabase mongoDB = server.GetDatabase("housing", credentials);
MongoCollection _houses = housing.GetCollection(houses);
var a = Query.WithinCircle("houseLocation", Double.Parse(latitude), double.Parse(longitude), maxDistance); //maxDistance is in radians
var cursor = _houses.FindAs<House>(a);
foreach (var b in cursor)
{
    //something
}

If I run it through debug I get, "enumeration yielded no results".

I have seen a lot of code samples and it seems that many use the Find method which I don't seem to have. If I type _houses.Find(query). I get a compilation error that it can't find a definition for Find.

I'm pulling my hair out because this should be pretty simple and it's frustrating as I'm trying 开发者_运维技巧to learn mongo. Any help would be greatly appreciated. Thanks!!


Most likely FindAs returned no results because no documents matched the query. We'd have to see some sample documents and the values of your parameters to determine why they didn't match the query.

The Find method is defined in the MongoCollection<TDefaultDocument> class, not in the the MongoCollection class (which is an abstract base class). You don't see the Find method because you defined your _houses variable as MongoCollection.

Looks like your collection object should have a default document type of House. I recommend declaring your _houses variable like this:

var _houses = mongoDB.GetCollection<House>("houses");
// or if you don't like var:
MongoCollection<House> _houses = mongoDB.GetCollection<House>("houses");

A further note on your geo query: if you ever plan on doing spherical queries make sure to store your data in (longitude, latitude) order. Most geo queries can work with longitude and latitude in either order (as long as you are consistent), but the spherical queries require that the order be (longitude, latitude), so I recommend everyone standardize on that right from the start.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜