using System; using System.Linq; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Driver; namespace MongoNullableTest { class Program { static void Main(string[] args) { GetUsers(UserType.Administrator); } static void GetUsers(UserType? userType = null) { var connectionString = ""; var database = new MongoClient(connectionString).GetDatabase(new MongoUrl(connectionString).DatabaseName); var filter = Builders.Filter.Empty; if (userType != null) filter = Builders.Filter.Where(u => u.UserType == userType); var users = database.GetCollection("user").Find(filter).ToList(); Console.WriteLine($"User count:{users.Count}"); Console.ReadKey(); } } public enum UserType { Standard = 0, Administrator = 1 } public class User { [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } public string Name { get; set; } public UserType UserType { get; set; } } }