Blog

  • How IoT is Transforming Smart Shopping
  • Turning Retail Pain into Smart Gain
  • Another Big Win for Axonize & Deutsche Telekom
  • Insights from 1,300 IoT projects in 2018 & What to Expect in 2019
  • Smart city orchestration in action – connecting all city smart apps
  • IoT Sensors & Bundles & Platforms, Oh My!
  • Break Your Sensors Out of Their Silos
  • Achieving in-transit visibility in complex supply chains
  • Case Study: How Megla is Implementing IoT to Unleash Data
  • Growing Gains: Microsoft on scaling to hundreds of microservices
  • Axonize launches partnership with Singtel and enters the Asian and Australian markets
  • Case Study: How Groupe Tera is Using IoT to Measure Air Quality Sensor Data
  • Case Study: Deutsche Telekom Selects the Axonize IoT Orchestration Platform
  • Case Study: How Optus is Using IoT to Disrupt the Retail Industry in Australia
  • Diving into Edge Computing
  • AXONIZE SELECTED AS ONE OF THE TOP IOT STARTUPS OF 2018
  • Case Study: Fast Food Chain Saves 27% on energy consumption
  • Case Study: Hotel Improves Efficiency & Customer Experience with IoT
  • Case Study: Presidential House Installs Comprehensive Monitoring of Mission Critical Server Room
  • POPULAR IOT PROTOCOLS 2018: AN OVERVIEW & COMPARISON [Updated]
  • Deutsche Telekom IoT Leadership Visits Bezeq & Axonize
  • Accelerating time-to-market by 90% with Microsoft Azure
  • Axonize Wins Deutsche Telekom Investment for Innovative IoT Platform
  • Using IoT Orchestration to Break Down the Silos
  • What is IoT orchestration?
  • How facility managers are "smartifying" their buildings for increased profitability
  • Case Study: How Bezeq is ‘Smartifying’ Kindergartens & Schools
  • The 4 keys to starting small and scaling successfully in IoT
  • IoT revenue is in the application development for service providers
  • Most Popular IoT Use Case? Smart Energy Management
  • Everything You Need to Know: Deloitte's The Building of the Future Meetup
  • Axonize named one of the top 10 most disruptive companies
  • What is an IoT Platform & When to Use One
  • Popular IoT protocols: An overview & comparison
  • Case Study: Leading Israeli service provider Bezeq chooses Axonize to deliver digital business services
  • The most frequently asked IoT questions
  • How System Integrators are growing their IoT business these days
  • The survey results are in: Integrators’ top roadblocks to IoT business growth
  • In It To Win IT: How to get to a live IoT project in 4 days
  • In it to win it: why system integrators should be taking over IoT
  • Joining Collections in MongoDB using the C# driver and LINQ
  • Simple or sophisticated? What kind of IoT platform do you need?
  • The Benefits & Downfalls of Using Azure Stream Analytics for IOT Applications
  • The Case for A Smart Campus, From Someone Who Would Benefit
  • The Top 3 Considerations in Evaluating and Selecting an IoT Platform

IntroA flexible IoT platforms needs a flexible database that can handle dynamic data, and can scale well.

MongoDB is probably the most popular NoSql Database out there and it’s relatively easy to use in conjunction with .Net with the official driver. It is a document DB which has many advantages in terms of schema-less/dynamic properties. The downside is that it is very difficult to combine data from various entities. In fact, up until recently it was impossible to do this in the database itself.

The MongoDB team recently introduced the new $lookup feature in its aggregation platform. This feature is the first time you can join two collections together, creating an array of type B in returned objects A.

I wanted to write a short post showing how the C# driver supports this new functionality when combined with LINQ.

Why MongoDB makes sense for an IOT platformWhy even use MongoDB in IoT platform?
MongoDB has two main strengths for IoT:

  1. IoT applications tend to generate a lot of sensor data – MongoDB scales for big data
  2. There is currently no standard for IoT data, no one protocol that all IoT devices conform to. This means that as an IoT platform, you have to be ready to get all sorts of schemas from your devices. Mongo has great support for dynamic data. Just throw anything at it and it will allow you to work with it. In a SQL server it would be a huge hassle to add support for totally dynamic properties.

Note: These advantages are not unique to MongoDB, many Document based NoSQL databases have them, however MongoDB is by far the most used of these kinds of databases.

PrerequisitesMongo versionThe $lookup operator was added to MongoDB in version 3.2, so make sure you are using that version of the database. To check what version of MongoDB you are using you can use the command db.version() in your mongo client of choice.
Driver versionThe C# driver must be at least 2.2.4. This is very important as version 2.2.3 has a bug that did not allow this to work.

ExamplesThe Mongo $lookup OperatorFrom the Official documentation:

{

$lookup:

{

from: <collection to join>,

localField: <field from the input documents>,

foreignField: <field from the documents of the “from” collection>,

as: <output array field>

}

}

So for example if I wanted to join my devices with a different collection of sensor readings, I would use this command:

 

db.devices.aggregate([{ “$lookup” : { “from” : “readings”, “localField” : “_id”, “foreignField” : “deviceId”, “as” : “joinedReadings”} }])

 

and the result will be something like this:

Joining Collections in MongoDB using the C# driver and LINQ

Note: this is a left outer join, it will return all devices.
Simple C# LINQ ExampleIn C# you can use the LINQ JOIN to generate this $lookup:
Joining Collections in MongoDB using the C# driver and LINQ

Example with Filtering on left JoinAs we can see you are passing in an IQueryable to the join for the $lookup, so you can also add other LINQ expressions to it before doing the join. Let’s update the example but this time only query for connected devices.

Joining Collections in MongoDB using the C# driver and LINQ

Unfortunately, there is no way to filter on the right side of the join (readings in our example). This is a limitation of the MongoDB implementation.

ConclusionHaving the ability to join collections is a wonderful addition to your toolbox as a MongoDB developer. However, the first solution should probably be to try to model your data in a way which does not need this ability. It has both performance implications and limitations on what you can do while joining. That being said, support through LINQ and C# is good once you figure out how to do it.

So, what does it take to successfully break down the silos and orchestrate data from different IoT devices and IT systems? Discover the essential building blocks of an orchestrated IoT application in this whitepaper.