using MongoDB.Driver; string connectionString = "mongodb://localhost:27017"; var client = new MongoClient(connectionString); var database = client.GetDatabase("mydb");
using MongoDB.Driver; string connectionString = "mongodb://localhost:27017"; var client = new MongoClient(connectionString); var database = client.GetDatabase("mydb"); var collection = database.GetCollectionIn this example, we obtain a reference to a collection by calling `GetCollection()`, passing the name of the collection as a generic parameter. We then create a new `BsonDocument` object to represent the document we want to insert into the collection. Finally, we call `InsertOne()` on the collection object to insert the document into the database. Package Library: MongoDB.Driver.("mycollection"); var document = new BsonDocument { { "name", "John Smith" }, { "age", 35 }, { "address", new BsonDocument { { "street", "123 Main St." }, { "city", "Anytown" }, { "state", "CA" }, { "zip", "12345" } } } }; collection.InsertOne(document);