the mongodb driver has it's limits and sometimes you need to compose advanced queries either by hand or using a query editor/composer. to be able to run those queries and inject values from your C# code, you're required to compose BsonDocuments or do string concatenation which leads to an ugly, unreadable, magic string infested abomination.
in order to combat this problem and also to couple your C# entity schema to raw queries, the library MongoDB.Entities offers a templating system based on tag replacement.
to couple this text query to your C# models and pass in the values for title and price, simply surround the parts you want replaced with < and > in order to turn them in to replacement tags/markers like this:
the templating system is based on a special class called Template. you simply instantiate a 'Template' object supplying the tagged/marked text query to the constructor. then you chain method calls on the Template object to replace each tag you've marked on the query like so:
varquery=newTemplate<Book>(@"
{
<Title> : '<book_name>',
<Price> : <book_price>
}").Path(b=>b.Title).Path(b=>b.Price).Tag("book_name","The Power Of Now").Tag("book_price","10.95");varresult=awaitDB.Find<Book>().Match(query).ExecuteAsync();
the resulting query sent to mongodb is this:
db.Book.find({Title:'ThePowerOfNow',Price:10.95})
the .Tag() method simply replaces matching tags on the text query with the supplied value. you don't have to use the < and > characters while using the .Tag() method. infact, avoid it as the tags won't match if you use them.
the .Path() method is one of many offered by the Prop class you can use to get the full 'dotted' path of a property by supplying a lambda/member expression. please see the documentation of the 'Prop' class here for the other methods available.
notice, that most of these 'Prop' methods only require a single parameter. whatever member expression you supply to them gets converted to a property/field path like this:
expression: x => x.Authors[0].Books[0].Title
resulting path: Authors.Books.Title
if your text query has a tag <Authors.Books.Title> it will get replaced by the resulting path from the 'Prop' class method.
the template system will throw an exception in the event of the following 3 scenarios.
the input query/text has no tags marked using < and > characters.
the input query has tags that you forget to specify replacements for.
you have specified replacements that doesn't have a matching tag in the query.
this kind of runtime errors are preferable than your code failing silently because the queries didn't produce any results or produced the wrong results.
hope the above text query template system has piqued your interest enough to go deeper into learning how to use mongodb with C#. the package MongoDB.Entities makes it extremely easy to communicate with mongodb server. every aspect of it's api has been documented on the official website. you can also checkout the source code on github:
A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management
MongoDB.Entities
A light-weight .net standard library with barely any overhead that aims to simplify access to mongodb by abstracting the official driver while adding useful features on top of it resulting in an elegant API surface which produces beautiful, human friendly data access code.
More Info:
please visit the official website for detailed documentation: