RDotLee
RDotLee

Reputation: 1113

MongoDB + C# Driver + Geospatial fields querying - How to define location property on entities

I'm running version 2.06 of Mongodb and version (1.5) of the C# driver supplied by 10Gen.

I want to have latitude and longitude coordinates on one of my entities so I can query entities using GeoNear or Query.WithinCircle.

I am adding a strongly typed Location object to my entity and then creating an index on the Location object that is hanging off my entity, but I can't get any GeoNear queries to work.

This is what my Location entity looks like.

public class Location
{
    [BsonDefaultValue(null)]
    public Nullable<double> lon { get; set; }

    [BsonDefaultValue(null)]
    public Nullable<double> lat { get; set; }  
}

This is how my entity in question looks.

  public class SomeEntity
  {
   public string Field1 {get; set;}

   // more fields here...

   public Location Loc {get; set;}
  }

I have found this link http://www.mongodb.org/display/DOCS/Geospatial+Indexing/ and see that my location field needs to be an array. What would my entity, SomeEntity, look like with an array field instead of my Location object?

I know this is simple I just can't figure it out! :-)

Thanks

Upvotes: 2

Views: 1459

Answers (1)

Robert Stam
Robert Stam

Reputation: 12187

You could try this:

public class SomeEntity
{
    public string Field1 {get; set;}

    // more fields here...

   public double[] Loc {get; set;}
}

Although your existing class should have worked (while arrays are recommended the only real requirement is that the object consist of two numbers). I probably wouldn't use Nullable<double> and just use double instead.

In what way was your geo query not working?

Upvotes: 4

Related Questions