Video Transcript (English)
Welcome! In this video, we’ll define BSON and explain how BSON documents are expressed in C# by using the C# class and BsonDocument class. Then we’ll structure a BSON document by using the MongoDB.Bson package.
Binary JSON, or BSON, is the data format that MongoDB uses to organize and store data.
BSON is optimized for storage, retrieval, and transmission across the wire. BSON is more secure than plain text JSON, which is vulnerable to JSON injection attacks. BSON also supports more data types than JSON.
MongoDB objects are represented in C# in two main ways: As a BsonDocument, As a C# class.
The first approach to working with BSON documents is to use the BsonDocument class provided by the MongoDB.Bson package. If you know the names and types of fields in the MongoDB document, you can recreate the document. This approach is useful if you have schemaless data, where the fields are unknown.
In this example, we import the MongoDB.Bson package, which gives us access to the BsonDocument class.
The BsonDocument specifies four fields and their values. It’s from a bank's database and includes an account_id with a numerical value, the account holder’s name, the account type—which is a checking account in this document—and the balance.
The downside to using BsonDocuments is that they’re not as easy to work with as C# classes.
A more robust approach to working with MongoDB documents is to create a C# class to represent the document and its properties. The driver translates to and from the MongoDB document and the class. So, working with the document is just like working with any other C# object. Creating a C# mapping class requires knowledge of the document schema, but offers a wider range of tools for working with MongoDB data.
In this course, we’ll create a C# class to represent a document.
In this example, we’ll create an Account class where each public property maps to a field in the MongoDB document. Because we typically use C# idiomatic naming, property names often do not exactly match the name of the field in the BsonDocument. In these cases, we use the BsonElement attribute to map the property to the Bson field name.
Here, we’re mapping the AccountId property to the account underscore id field in the Bson document.
We’ve done the same thing on each of the properties.
Let’s examine a few other attributes that we’re using.
The BsonId attribute specifies a field that must always be unique. Every document stored in MongoDB must have a unique Id. If you don't provide an id in your code when creating a new document, MongoDB will automatically generate one for you.MongoDB also creates an index on this field.
The BsonRepresentation attribute lets us map a C# type to a specific BSON type.
Congratulations! You now know the two primary ways to create MongoDB documents in C#.
The BsonDocument Object class provides a generic way to represent a document. It’s useful when working with schemaless data.
C# classes provide type safety and make working with MongoDB data much easier.