MongoDBTutorial
Quick Start: C# And MongoDB - Starting And Setup
Summary
This quick start guide demonstrates the initial setup for connecting C# applications to MongoDB. The demonstration uses Visual Studio 2019 on Windows 10 and connects to a MongoDB Atlas cluster that has the sample dataset pre-installed. Setup involves creating a .NET Core console application named MongoDBConnectionDemo and installing the MongoDB.Driver version 2.9.1 via NuGet’s Manage Packages for Solution. The code adds a using MongoDB.Driver directive, creates a MongoClient with the Atlas connection string, retrieves the list of databases using ListDatabaseNames, and prints each database name to the console in just over 20 lines. This article is the first in a Quick Start series that will cover creating, reading, updating, and deleting documents, mapping MongoDB documents to C# objects, using the MongoDB aggregation pipeline, and integrating LINQ queries with MongoDB.
Context
This is the first article in a Quick Start series on using C# with MongoDB. It targets developers familiar with C# who want to connect to MongoDB Atlas. The series will cover CRUD operations, object mapping, aggregation, and LINQ.
Approach / What changed
Create a .NET Core console application, install the MongoDB.Driver NuGet package version 2.9.1, add a using MongoDB.Driver directive, create a MongoClient with the Atlas connection string, retrieve the list of databases using ListDatabaseNames, and print each database name to the console.
Takeaways
The MongoDB.Driver for C#/.NET can be installed via NuGet, and the latest stable version at the time of writing is 2.9.1.
Connecting to MongoDB Atlas requires a connection string passed to a MongoClient object, after which database names can be retrieved with ListDatabaseNames.
The entire connection demonstration program is just over 20 lines of code, showing the simplicity of the setup.
FAQ
How do I install the MongoDB driver for C#?
Use NuGet Package Manager in Visual Studio to browse for MongoDB.Driver and install version 2.9.1.
How do I connect to MongoDB Atlas from C#?
Create a MongoClient with the Atlas connection string, then call ListDatabaseNames to retrieve and print the database list.
What will future articles in this series cover?
Future articles will cover CRUD operations, mapping MongoDB to C# objects, aggregation, and using LINQ with MongoDB.
Source and freshness
- Source
- mongodb.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.