Table of Contents

Count entities

there are a couple of ways to get the count of entities stored in a collection.

Count estimated total

var count = await db.CountEstimatedAsync<Author>();

you can get a fast estimate of total entities for a given entity type at the expense of accuracy. the above will give you a rough estimate of the total entities using collection meta-data.

Count total entities

var count = await db.CountAsync<Author>();

the above will give you an accurate count of total entities by running an aggregation query.

Count matches with an expression

var count = await db.CountAsync<Author>(a => a.Title == "The Power Of Now");

Count matches with a filter builder function

var count = await db.CountAsync<Author>(b => b.Eq(a => a.Name, "Eckhart Tolle"));

Count matches with a filter definition

var filter = DB.Filter<Author>()
               .Eq(a => a.Name, "Eckhart Tolle");

var count = await db.CountAsync(filter);

Counting children of a relationship

you can get how many entities are there in the opposite side of any relationship as shown below:

var authorCount = await book.Authors.ChildrenCountAsync();
var bookCount = await author.Books.ChildrenCountAsync();