using System; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Driver; namespace MongoTest { internal class Program { static void Main(string[] args) { var collection = new MongoClient("mongodb://localhost:C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==@localhost:10255/admin?ssl=true") .GetDatabase("foodb") .GetCollection("barcol") .OfType(); var foo = new Foo { Title = "Foobar2000" }; collection.InsertOneAsync(foo).Wait(); Console.WriteLine(foo.Id); Console.ReadLine(); } } /// /// Class for generic database entity with identifier. /// /// [BsonKnownTypes(typeof(Entity))] public class Entity { /// /// Gets or sets the identifier. /// /// The identifier. [BsonId] //[BsonIgnoreIfDefault] public T Id { get; set; } } /// /// Class for database entity with guid identifier. /// [BsonKnownTypes(typeof(Foo))] public class Entity : Entity { } public class Foo : Entity { /// /// Gets or sets the title. /// /// The title. public string Title { get; set; } } }