The simplest, type/refactor-safe option is to use LINQ
* with AsQueryable
:
var collection = database.GetCollection<ApplicationViewModel>("Applications");
var exists = collection.AsQueryable().Any(avm => avm.Name == applicationName);
This will create a count command and verify it's higher than zero.
In certain cases (where performance is an issue) instead of counting all the matching documents you can simply tell MongoDB
to get the first and check whether there is one:
var collection = database.GetCollection<ApplicationViewModel>("Applications");
var exists = collection.AsQueryable().FirstOrDefault(avm => avm.Name == applicationName) != null;
As Robert Stam pointed, both MongoCollection.Exists
and Query.Exists
are irrelevant in this case.
*As of version 1.4 (2012-03-27) the driver supports LINQ
queries (translated to mongo queries, so there are no memory concerns).