DEV Community

M. Höller
M. Höller

Posted on

MongoDB Wizards - your wands gets an upgrade!

MongoDB 3.4 has been announced, the new version provides some real cools features. I like to show you some magic you can do with them. I have not yet tried all of them, so the list is not complete but I will post updates here. Don't be afraid you do not need to learn any complex charm - everything is kept easy as it was, though the features mature more and more..

The aggregation pipeline got in 3.4 quite a bit of enhancements. Like more operators, expression, enhancements for the use of array elements, string handling and support for branching.

For the new array operators I like to highlight two which I was waiting for: $in and $indexOfArray. They do what their names promise $in returns a boolean that indicates if a given value is in an array, $indexOfArray returns the array index of the first occurrence of a given value.

Concerning strings there where a lot of UTF-8 handling enhancements plus, one of my favorites, $split. Guess what it is doing… Correct, it splits a string by a given delimiter and returns the splits in an array.

Also very interesting is a new option to control the flow of expressions. You can use $switch to evaluate a set of case expression. When it finds an expression which evaluates to true $switch will branch into it and execute the associated expression and break out. So nothing new for developers but cool to have it directly in the aggregation pipeline. Here is a quick view on the syntax, you may add it to you book of charms :

   $switch: {
        branches: [
            { case: <expression>, then: <expression> },
            { case: <expression>, then: <expression> },
            
        ],
    default: <expression>
    }
Enter fullscreen mode Exit fullscreen mode

You can follow this link to get further information and a complete list of new features for the aggregation pipeline.

Actually also part of the aggregation pipeline but worth an extra part is the ability to do recursive searches. The magic word is $graphLookup With its help you will never get a headache any more when you need to answer a shortest path question. There is a interesting Webinar: Working with Graph Data in MongoDB which explains in detail how to use it an quite a bit of information around it. For the fast grasshoppers skip the first 20 min and jump in there. But be warned to rush in always means to loose something – the first part provides a lot of information.

With MongoDB 3.4 the connection to the world has been improved significantly. The BI connector has been reworked and makes it now much easier to make use of your data. The new version also includes an update for the Apache Spark connector . If you have not yet check that out then it is time now. You can use the power of Apache Spark and add now all the advantages of MongoDBs aggregation pipeline and the fast in memory prepossessing, sharding to overcome some slownesses of Sparks Resilient Distributed Datasets (RDD).

Looking backwards you might got the impression that there are improvements all over the place and again – you are correct! I mentioned only new features which I started working with or which I could directly use to get an improvement. You may pick others from the documents I have linked in the text. I would not say that 3.4 is changing the world but it changed my development.. Why? Look at the little features to the left and right. E.g. the $split prior to 3.4 you queried the collection, massaged the result with javascript, and might started a further query. Now you can do all in one. So what is the change? It is simpler code, less lines – just better readable and so better maintainable and it is a little bit faster since you act natively on the db. A $split is not so significant but if you go for $graphLookup you will realize a big difference.

So, MongoDB wizards, do good magic and use the full power of your upgraded wands! Lumos Maxima!

{ name : "Michael Höller",
eMail : "info@akazia.de"}

Top comments (0)