Lesson 2: Inserting a Document in C# Applications / Learn

Inserting a Document in C# Applications

Review the following code, which demonstrates how to insert a single document and multiple documents into a collection.

Insert a Single Document

Use the GetCollection() method to access the MongoCollection object, which is used to represent the specified collection. For example:

var accountsCollection = database.GetCollection<Account>("account");


C# Class

Use the InsertOne() method on the accountsCollection object to insert a document into the collection. Within the parentheses of InsertOne(), include an object that contains the document data. Here's an example:

var accountsCollection = database.GetCollection<Account>("account");

var newAccount = new Account
{
    AccountId = "MDB829001337",
    AccountHolder = "Linus Torvalds",
    AccountType = "checking",
    Balance = 50352434
};

accountsCollection.InsertOne(newAccount);



BsonDocument

Use the BsonDocument class to insert a document into the collection. Within the parentheses of InsertOne(), include an object that contains the document data. For example:

var accountsCollection = database.GetCollection<BsonDocument>("account");

var document = new BsonDocument
{
   { "account_id", "MDB829001337" },
   { "account_holder", "Linus Torvalds" },
   { "account_type", "checking" },
   { "balance", 50352434 }
};

accountsCollection.InsertOne(document);


Async

Use the InsertOneAsync() method on the accountsCollection object to insert a document asynchronously into the collection. Within the parentheses of InsertOne(), include an object that contains the document data. For example:

public async Task AddAccount()
{
   var newAccount = new Account
   {
       AccountId = "MDB829001337",
       AccountHolder = "Linus Torvalds",
       AccountType = "checking",
       Balance = 50352434
   };

   await accountsCollection.InsertOneAsync(document);
}


Insert Many Documents

This section shows three ways to insert multiple documents into a collection.


C# Class

Use the InsertMany() method on the accountsCollection object to insert multiple documents into the collection.

var accountsCollection = database.GetCollection<Account>("accounts");

var accountA = new Account
{
    AccountId = "MDB829001337",
    AccountHolder = "Linus Torvalds",
    AccountType = "checking",
    Balance = 50352434
};

var accountB = new Account
{
    AccountId = "MDB011235813",
    AccountHolder = "Ada Lovelace",
    AccountType = "checking",
    Balance = 60218
};

accountsCollection.InsertMany(new List<Account>() { accountA, accountB });


BsonDocument

Use the BsonDocument class to insert a document into the collection. Within the parentheses of InsertMany(), include an object that contains the document data. For example:

var accountsCollection = database.GetCollection<BsonDocument>("account");

var documents = new[]
{
    new BsonDocument
            {
                { "account_id", "MDB011235813" },
                { "account_holder", "Ada Lovelace" },
                { "account_type", "checking" },
                { "balance", 60218 }
            },
    new BsonDocument
            {
                { "account_id", "MDB829000001" },
                { "account_holder", "Muhammad ibn Musa al-Khwarizmi" },
                { "account_type", "savings" },
                { "balance", 267914296 }
            }
};

accountsCollection.InsertMany(documents);


Async

Use the InsertManyAsync() method on the accountsCollection object to insert multiple documents asynchronously into the collection. Here's an example:

public async Task AddAccounts()
{
   var accountA = new Account
{
    AccountId = "MDB829001337",
    AccountHolder = "Linus Torvalds",
    AccountType = "checking",
    Balance = 50352434
};

var accountB = new Account
{
    AccountId = "MDB011235813",
    AccountHolder = "Ada Lovelace",
    AccountType = "checking",
    Balance = 60218
};

   await accountsCollection.InsertManyAsync(new List<Account>() { accountA, accountB });

}