Here’s the class I am mapping to MongoDB:
public class Thing
{
[BsonId]
public ObjectId Id { get; set; }
public string Foo { get; set; }
public string Bar { get; set; }
}
Saving it to the collection is great:
var collection = db.GetCollection<Thing>("things");
collection.Save(new Thing() {Foo = "one", Bar = "two"});
But when I want to search for it, I have to explicitly specify element names as strings:
var collection = db.GetCollection<Thing>("things");
collection.Find(Query.EQ("Foo", "one"));
There is no way for the compiler to help with these queries. I have to reference my class file, copy the element name, and paste it into the query. If I change the element name later, the compiler won’t make sure that I changed it in the queries, let alone change it for me. Is there a better way?
There is currently no support from a compiler perspective for querying in v1.3.1 of the official driver. We are currently working on LINQ support which should help with the mapping to element names.(https://jira.mongodb.org/browse/CSHARP-91). If you would like to see this outside LINQ please file an enhancement request at https://jira.mongodb.org/browse/CSHARP