Table of Contents

Find queries

Several overloads are available for finding entities as shown below.

Find one by ID

var author = await db.Find<Author>().OneAsync("ID");

Find many by lambda

var authors = await db.Find<Author>().ManyAsync(a => a.Publisher == "Harper Collins");

Find many by filter

var authors = await db.Find<Author>()
                      .ManyAsync(f=> f.Eq(a=>a.Surname,"Stark") & f.Gt(a=>a.Age,35));
Tip

All the filter definition builder methods of the official driver are available for use as shown above.

Find by 2D coordinates

var cafes = await db.Find<Cafe>()
                    .Match(c => c.Location, new Coordinates2D(48.857908, 2.295243), 1000)
                    .ExecuteAsync();

See this tutorial for a detailed walkthrough.

Find by aggregation expression ($expr)

var authors = await db.Find<Author>()
                      .MatchExpression("{$gt:['$TotalSales','$SalesGoal']}")
                      .ExecuteAsync();
Tip

Aggregation expressions lets you refer to properties of the same entity using the $ notation as well as enable you to use aggregation framework operators in find queries.

Advanced find

Sorting, paging and projecting

var authors = await db.Find<Author>()
                      .Match(a => a.Age > 30)
                      .Sort(a => a.Age, Order.Descending)
                      .Sort(a => a.Name, Order.Ascending)
                      .Skip(1).Limit(1)
                      .Project(a => new Author { Name = a.Name })
                      .ExecuteAsync();

The search criteria is specified using .Match() which takes either an ID, lambda expression, filter expression, geospatial, or full/fuzzy text search query.

Sorting is specified using .Sort() which takes in a lambda for the property to sort by and in which order. .Sort() can be used multiple times in order to specify multiple sorting stages. When doing text queries, you can sort the results by mongodb's 'meta text score' by using the .SortByTextScore() method.

How many items to skip and take are specified using .Skip() and .Limit()

Projections

To avoid the complete entity being returned, you can use .Project() with a lambda expression to get back only the properties you need as shown above. It is also possible to use projection builder methods like so:

.Project(p => p.Include("Name").Exclude("Surname"))
Tip

To be able to chain projection builder methods like above, please add the import statement using MongoDB.Driver; to your class.

Projection with exclusions

It is also possible to specify an exclusion projection with a new expression like so:

var res = await db.Find<Author>()
                  .Match(a => a.ID == "xxxxxxxxxxx")
                  .ProjectExcluding(a => new { a.Age, a.Name })
                  .ExecuteSingleAsync();

Doing so will return an Author entity with all the properties populated except for the Age and Name properties.

Project to a different type

In order to project to a different result type than the input entity type, simply use the generic overload like so:

var name = await db.Find<Author,string>()
                   .Match(a => a.ID == "xxxxxxxxxxx")
                   .Project(a => a.FirstName + " " + a.LastName)
                   .ExecuteSingleAsync();

Execute

No command is sent over the wire to mongodb until you call one of the following Execute*() methods:

ExecuteCursorAsync(): Gets a cursor you can iterate over instead of a list of entities.

ExecuteAsync(): Gets a list of matched entities or default value if nothing matched.

ExecuteSingleAsync(): Gets only 1 matched entity and will throw an exception if more than 1 entity is matched, or default value if nothing matched.

ExecuteFirstAsync(): Gets the first of the matched entities, or default value if nothing matched.

ExecuteAnyAsync(): Gets a boolean indicating whether there were any matches.