Introduction to MongoDb with .NET part 28: aggregation in the .NET driver using loose typing and AppendStage

Introduction

In the previous post we started looking into MongoDb aggregations via the .NET driver. Our first example was a single $sort stage using the AppendStage function. The AppendStage function is generic in the sense that it can accept just any type of aggregation stage to be added to the pipeline. We also introduced the BsonDocument object which offers a way to describe MongoDb documents in a loosely typed way.

In this post we’ll continue our discussion and build the pipeline we’re targeting.

Read more of this post

Introduction to MongoDb with .NET part 27: aggregation in the .NET driver

Introduction

In the previous post we looked at various other aggregation operators in MongoDb. With $first and $last we can find the highest and lowest member in a sorted collection. If you need to find e.g. the city with the highest population or the country with the lowest number of pets per capita then these operators will probably figure in the aggregation pipeline. The $unwind stage operator blows up the members of an array so that we can run a query on those members in an easier way. Without $unwind it’s difficult to access the individual members of an array. Note that $unwind can dramatically increase the result set of documents.

We’ve now gone through the most important aggregation operators in MongoDb. It’s time to turn our attention to how aggregations can be written in the .NET driver.

Read more of this post

Introduction to MongoDb with .NET part 26: more aggregation types and examples

Introduction

In the previous post we looked at how to add multiple stages to a MongoDb aggregation pipeline. We saw that it was easy and straightforward to extend the stages array of the pipeline. It’s important to remember that each stage acts upon the set of documents produced by the previous stage. We can also have the same stage type appear more than once in the pipeline, i.e. there’s no limit saying we can only have one $group operation in a pipeline. The various stages can also appear in various order, we don’t have to follow some type of ordering either.

In this post we’ll continue where we left off and look at a couple more aggregation operators and examples.

Read more of this post

Introduction to MongoDb with .NET part 25: adding multiple stages to the aggregation pipeline

Introduction

In the previous post we continued our discussion of the grouping stage in the MongoDb aggregation pipeline. We looked at a couple of $ operators that can be used in aggregations, such as $min, $avg and $addToSet. We also saw how to use compound IDs to add multiple grouping keys. Compound IDs are also useful when we want to give an explanatory field to the compulsory _id property so that it’s clear what the grouping key is.

In this post we’ll start adding multiple stages to the pipeline. So far we’ve had simple aggregation pipelines with one stage but there’s nothing stopping us from adding more to the stage array. It’s also possible to add multiple instances of the same stage type.

Read more of this post

Introduction to MongoDb with .NET part 24: aggregations continued

Introduction

In the previous post we looked at our first aggregation example. We grouped the zipcodes collection by state and number of inhabitants in each state. A grouping stage is represented by the $group operator and it’s followed by a number of arguments. We went through the process that MongoDb performs to produce the final set of documents. Essentially it performs a series of upsert operations on the starting collection.

In this post we’ll look at some other query examples.

Read more of this post

Introduction to MongoDb with .NET part 23: first aggregation examples

Introduction

In the previous post we introduced the topic of aggregations in general. Aggregations in MongoDb are similar in purpose to the GROUP BY clause in SQL. They help us analyse the data stored in the database so that understand things like long term business trends, user and customer behaviour, which part of our business is making losses and similar. Aggregation queries are added to the aggregation pipeline using $ operators and the documents are passed from one stage to the next in the pipeline. We will get the aggregation result at the end of the pipeline.

In this post we’ll look at our first aggregation example.

Read more of this post

Introduction to MongoDb with .NET part 22: starting with aggregations

Introduction

In the previous post we looked at bulk writes in the MongoDb .NET driver. We saw how various model allowed us to package together a number of insert, delete and update operations into one bulk write. The operations in the group are sent to the database to be executed at the same time.

We have therefore finished the discussion of the most important CRUD operations in this series. We are now familiar with the basics of SELECT, UPDATE, INSERT and DELETE both in the Mongo shell and the .NET driver. In this post we’ll start looking into a very different form of querying, namely aggregations. Aggregations are similar to grouping techniques in standard SQL using the GROUP BY clause. However, MongoDb aggregations are very different as far as the concept and the query syntax are concerned.

Read more of this post

Introduction to MongoDb with .NET part 21: bulk writes in the MongoDb driver

Introduction

In the previous post we discussed how to delete documents with the MongoDb .NET driver. After all we’ve learnt about filter definitions and various options it was really easy to go through the various deletion-related functions.

We’ve covered all the CRUD operations in .NET. I’d like to show one more database manipulation option related to CRUD before we move on: bulk writes.

Read more of this post

Introduction to MongoDb with .NET part 20: deletions in the MongoDb driver

Introduction

In the previous post we discussed how to perform updates via the MongoDb .NET driver. Update operations can be divided into replacements and “true” updates. A replacement is where an existing document is replaced by another and only the _id field stays constant. A “true” update is really how we normally think updates should happen, i.e. only specific properties are overwritten in the matching document(s). Both update types are represented by a number of methods in the driver such as UpdateOne or FindOneAndReplace or UpdateMany.

In this post we’ll close the CRUD circle and look at deletions in the driver. After all we’ve learnt about filter definitions and options it will be a breeze to go through deletions.

Read more of this post

Introduction to MongoDb with .NET part 19: update operations in the MongoDb driver

Introduction

In the previous post we discussed how to insert new documents via the MongoDb .NET driver. We saw that it wasn’t particularly complicated to use the various Insert methods. We just need to provide the object to be inserted and that’s about it.

In this post we’ll look at how to update documents. We have already seen how to do updates via the Mongo shell and now it’s enough to find how they are represented in C#.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

ARCHIVED: Bite-size insight on Cyber Security for the not too technical.