Docs Menu
Docs Home
/ / /
C#/.NET

Usage Examples

On this page

  • Overview
  • How to Use the Usage Examples
  • Example Classes

Usage examples provide convenient starting points for popular MongoDB operations. Each example provides the following information:

  • A code snippet that shows how to perform the operation in synchronous and asynchronous frameworks

  • A link to a fully runnable console application using the operation

  • The expected result after running the example

Tip

Whether you use a synchronous or asynchronous framework in your application depends on your use case. Synchronous calls are more suitable for simple query workflows or when you must implement sequential logic. Consider using asynchronous calls if your application relies on multiple concurrent database requests or if your program doesn't require an immediate response from the database to continue executing.

We encourage experimenting with both approaches to determine the most suitable framework for your purposes.

These examples use the sample datasets provided by Atlas. You can load them into your database on the free tier of MongoDB Atlas by following the Get Started with Atlas Guide or you can import the sample dataset into a local MongoDB instance.

Once you have imported the dataset, you can copy and paste a usage example into your development environment of choice. You can follow the Quick Start to learn more about getting started with the MongoDB .NET/C# Driver. Once you've copied a usage example, you'll need to edit the connection URI to get the example connected to your MongoDB instance:

// Replace the following with your MongoDB deployment's connection string.
private static string _mongoConnectionString = "<connection string>";

For more information about connecting to your MongoDB instance, see the Connection Guide.

The usage examples in this section show how to perform operations on documents in the restaurants collection. The examples use the following Restaurant, Address, and GradeEntry classes to model the data in this collection:

public class Restaurant
{
public ObjectId Id { get; set; }
public string Name { get; set; }
[BsonElement("restaurant_id")]
public string RestaurantId { get; set; }
public string Cuisine { get; set; }
public Address Address { get; set; }
public string Borough { get; set; }
public List<GradeEntry> Grades { get; set; }
}
public class Address
{
public string Building { get; set; }
[BsonElement("coord")]
public double[] Coordinates { get; set; }
public string Street { get; set; }
[BsonElement("zipcode")]
public string ZipCode { get; set; }
}
public class GradeEntry
{
public DateTime Date { get; set; }
public string Grade { get; set; }
public float? Score { get; set; }
}

Note

The documents in the restaurants collection use the snake-case naming convention. The examples in this guide use a ConventionPack to deserialize the fields in the collection into Pascal case and map them to the properties in the Restaurant class.

To learn more about custom serialization, see Custom Serialization.

Back

What's New