MongoDB:Using MongoDB in C# and .Net Core #mongodb

preview_player
Показать описание
MongoDB: Using MongoDB in C# and .Net Core
Introduction
This lesson will discuss BSON objects and how to map them to a C# object. It will also introduce a database and its C# equivalent which we will use for learning purposes.
We’ll cover the following
Mapping BSON to Strongly Typed C# Object
Example Database
C# Equivalent
Explanation

Mapping BSON to Strongly Typed C# Object
If you have read the previous chapters, you will have learned that MongoDB stores documents in BSONformat, which are basically binary JSON files (ok, it is a little bit more complicated than that).
When we use .NET driver, we consume documents through BsonDocument. What we want to do in this exercise is to map those BsonDocuments to strongly typed C# objects. But before that, let’s see what our database, and its entities, look like.

Example Database
For the purpose of this exercise, we will use a database called — blog.
This database has a collection, users, which contains a document for each user.
The JSON document for an individual user would look like something like this:

Create & Read Operations
This lesson teaches how to create the class used to access MongoDB and the methods to perform the basic CRUD operations is C#.
We’ll cover the following
Users Repository
Create Users
Implementation
Read Users
Implementation

Users Repository
The goal of this lesson is to create a class which will give us the ability to do simple CRUD operations on the users collection.
The first thing we need to do is connect to the database from our application. The easiest way to do this is by using the MongoClient class, available via the MongoDB driver that has already been installed for you. (its constructor requires a connection string, which we will provide).
Alternatively, MongoClientSettings class, available via the MongoDB driver, can be used as it provides various possibilities. One such possibility is the ClusterConfiguration property, which is of the ClusterBuilder type and used for configuring clusters.
Other classes that we need to use are MongoDatabase, to access defined databases (blog in this case), and MongoCollection, to access defined collections (users in this case).
Рекомендации по теме
visit shbcf.ru