A NoSql database provides a different way of storing and handling data then the classical sql. While in a relational environment you have to split your models into logical components and store them in different tables using relations to define how will they be grouped together, in NoSql you work with json-like formats and this allows you to store entire documents.
Designing a NoSQL database is simpler and using it provide several advantages like easier horizontal scaling to clusters of machines and finer control over availability. Data structures used by NoSQL databases are more flexible than relational database tables but if it is actually suitable for your needs…well that depends on your project.
In this article I will present several ways of using filters for retrieving data from mongo using the MongoDB C# driver. While there is some documentation available on the official site, I found that it’s easier to dive in to the actual driver’s implementation and check out how are the components that I need defined.
You can download the Mongo Driver source code from the following github page. And I strongly recomend you to do so when learning because it offers a greater insight over the internal processes.
I will continue this article by simply showing you how to use the mongo client to connect to the database, how to get the mongo collection for our book and then several ways you can define filters for the GET method.
To find out how to install and configure mongo, there is a great article provided by which offers a clear and detailed description of the process. Just follow this linkÂ
We will start by defining our model. For this example I will create a simple Book class with several properties.
Our book will have 4 properties. Notice that above each property I added some attributes that will guide the mongo driver.
BsonElement – You can opt out of using this attribute and the driver will pick up the name from the property name. However I prefer using these attributes for database consistency. If somewhere in the process I decide that a property should have a different name I will want to be able to reference the existing document properties so I prefer to keep naming them manually.
BsonId – Each document in mongo must have an Id. If you do not provide a field for it mongo will automatically create one for you. However we want to be able to access the Id. This attribute will link the Id from the database to the class property.
BsonRepresentation – In our example this attribute is not relevant, but it’s important to have this option in mind. The default Id type in mongo is an objectId. However it’s possible that for your project you consider that you want to use another type, like a string. To do so you must set the ObjectType to string. BsonRepresentation let’s you change how the document property is represented in the class.
1
2
3
[BsonId]
[BsonRepresentation(BsonType.String)]
publicstringSId{get;set;}
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
publicclassBook
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
publicObjectIdId{get;set;}
[BsonElement("Name")]
publicstringName{get;set;}
[BsonElement("Author")]
publicstringAuthor{get;set;}
[BsonElement("InStock")]
publicboolInStock{get;set;}
}
To connect to the database and get our book collection there are several steps to meet:
Define the server address (the default values are 127.0.0.1 with port 27017)
You can use the amazing free tool Robo3TÂ to see your database. Our collection has now 4 entries and you can see it’s structure:
1. Using Linq Expressions syntax
You can use linq expression to filter data. Mongo driver will convert these expressions into valid mongo syntax and pass it along through the pipeline.
2. Using the Builders to create a filter definition
FilterDefinitions are a fast and intuitive way to create filters. They require less processing then using expressions and offer a lot of methods for you to use.
Here I created 2 filters (one for each property) and I combined them using the And filter:
3. Using ExpressionFieldDefinitions and combining filters with bitwise &
ExpressionFieldDefintion – this defines a way of letting mongo know what property is it targeting by using a lambda expression
Bitwise Add – If you take a peek in the filter definition you can see that the driver has overriden bitwise operators. This means that we can easily combine filters using the & operator instead of calling the Builders<T>.Filters.And method.
FilterDefinitions require a way of knowing what property is it targeting. StringDefintion allows you to state the name of the property you are targeting
You have a lot of features to chose from when defining filters. One example is using the IN operator which, similar to SQL, let’s you match a value in a list of values.
Why locking in the first place? Because in many scenarios we need to guarantee the consistency of the data, this means that we will not allow for one user changing data that another user hasRead more
Click to download the source file Yet another article about MONGO! This article is about retrieving data in mongo and tweaking the retrieval method to accommodate some project requirements. I am not an expert inRead more
With mongo some operations will return a dynamic object, usually wrapped in an expando object. To deserialize the expando object in the target class is quite simple, I actually looked in the source code toRead more
The mongo driver offers a rich and complete infrastructure to create rich domain entities out of the box. The bson serializer integrated with the mongo driver can tackle all types of entities, it can deserializeRead more
With mongo you can enrich you queries with projection definitions, making it easy to limit the data you retrieve from the database and also shape the data according to your projects preferences. I will showRead more