Over the past couple of weeks I’ve been having some rather frustrating experiences with Microsoft’s Data Pipelines which falls under the Data Factory part of Microsoft Fabric. How Microsoft names their products can also cause problems when looking for support. You’ll chance on the same conventions and naming being used in other Microsoft products which makes it harder to get to a solution.
Anyway back to the task at hand. I needed to import data from Azure Cosmos MongoDB into a Microsoft Fabric Lakehouse where we could query the data and do all that great stuff you end up doing with data to make sense of it and make business decisions. In my case I wanted to connect it to PowerBI.
This article is more for my own reference, but if its of vale to you or you have any comments or suggestion’s I would love to hear from you.
Firstly I wanted to import a collection so I went into https://app.fabric.microsoft.com and created a new Data Pipeline. I didn’t use the data assist option (more on this later). I just dragged the Copy data task onto the Pipeline canvas and selected my data source.
Connecting to Mongo

In this instance my data source was MongoDB NoSQL.

This probably isn’t the most straight forward process to setup this data source. Looking at various forums people do struggle with it especially if you are coming to it from a Cosmos DB implementation of MongoDB and it took me several attempts to get this right.

For the server you want to input the url to your server followed by a colon and its port number as hinted to in the server part. As you enter your server name it will rather helpfully try to populate the Connection name for you which depending on the name of your server may look a bit untidy. You can change this to something more meaningful before you save it, at the time I wasn’t able to change this name after I had saved it.
Authentication, you’ll get this from Azure if you are using Cosmos DB for MongoDB like I was.

You will use the username given here in the username field on the Mongo connection settings. For the password I chose to use Read-only Keys. As I would only be reading data not writing it back. Use the Primary Password. You have two here so you can cycle them and use the second one if you are regenerating the primary password.
After you have set these up you should be able to connect and choose your database from a drop down list. I discovered this sometimes didn’t work and needed a refresh. I was not entirely sure why, maybe it timed out?
Importing a Mongo Collection with CopyData
Now that you have connected to MongoDB you can map the data you wish to import into LakeHouse. In my case this would be the first part of the transformation of the data I was importing. The table created by this data will later be transformed by a Dataflow (more on this later) into another table.

To get the above view click on your Copy Data task and then on the Source tab. This will allow you to select your collection name from your MongoDB database. After you have selected it, you may want to preview your data this will give you an idea of what you are dealing with when it comes to transforming the data.
Next select the Destination tab. This is where you will select the LakeHouse you want to place your data, if you haven’t already created a Lakehouse you should probably create one now.

Choose the table in your Lakehouse you wish to send your data, if you don’t have one put a name in here which you wish to call your table and click New Table (it will create it for you). In my case this will be a temporary table from where I will collect data to be transformed by a Dataflow. Be sure to select Overwrite otherwise every time you run your transformation, data will just be appended to it.
Select the Mapping Tab

Now this is where things start to get interesting. I enabled “Allow data truncation” because several times I ran transformations some data was far too big which would cause errors (I wasn’t able to find a work around to this). I ignored all Type conversion settings, partly because they didn’t work very well at all. It caused me no end of trouble, I also discovered that while using it Microsoft changed things that would one day work and then the next stop working. I decided to flatten all columns as the Copy Data task was not very good at translating JSON arrays into columns (this surprised me). Sometimes it would get it right with some data or partially do transforms but then completely ignore others. There didn’t seem to be any logic to it and its what eventually led me to follow up the Copy Data task with a Dataflow (more on this later).
You can see in the table above that I mainly stuck to mapping data to either a string or Boolean. Copy Data didn’t seem be able to handle how integers were stored in MongoDB (this may all have changed by now). So just to be clear integers, strings and dates were all transformed into strings. Booleans were transformed into Booleans.
On the Settings tab I select Auto for Intelligent throughput optimisation.
You can now run your transformation using the Run icon located at the top of the screen next to the Validate button.
Transforming data with a Dataflow
Now that I had some raw data inside Lakehouse I need to transform the data so I could use it. The data I imported in the Copy Data task above had raw JSON in many of its fields.
Go back into your App Fabric workspace and create a DataFlow Gen 2 from the “New” menu located at the top left hand corner of the screen.

Select the Get Data menu option and from the dropdown select the “More…” option at the bottom of the list.

You should now see a list of Lakehouse data. Select the Lakehouse you copied data to in the previous step and then select the table you copied your data to in the previous step and select “Create”
You will be presented with the following.

You can see the createdAt and the updatedAt have come through in their raw format from MongoDB (ie this is how MongoDB stores its dates). We are now going transform those dates into a date type for Lakehouse.
Using Transform JSON
Right click on the column and select “Transform column” from the menu that appears. Now select “Text transforms” followed by JSON

This will create a step in the “Applied steps” window. You can rename the step that has been created in the Applied Steps window to make more sense to you later.

You will also notice our createdAt is now a record. Next you will want to click on the little icon (see image below) next to the createdAt to drill down into the record it has created. You can see in the dialogue that’s opened that it has detected one field. You can click OK here so we get to the actual date.

In the next step you can see it is now showing us the date, however Mongo DB stores the date in Unix epoch milliseconds. We will now need to transform this.

(optional) Right click on the column and rename it from the dropdown menu that appears. In my case I renamed it to createdAt.
At the top of the screen select the “Add column” tab followed by the Custom column option

In the dialogue box that appears we are now going to add a new column using the data from the column we transformed in our previous step. Basically we are going to transform the epoch date into a Date/Time column

So basically we have given our new column the name CreatedAtDate and we have applied the following custom formula.
#datetime(1970, 1, 1, 0, 0, 0) + #duration(0, 0, 0,Int64.From([createdAt])/1000)
Click OK once applied
This will transform the epoch date into a Date/Time object we can use in Lakehouse. You can see in the formula we have placed “createdAt” from the previous step as our argument.

As you will see we now have a brand new column with a date time we can use in Lakehouse. You can apply the same JSON text transform to all of your JSON fields.
Publish your transformed data
After you have the data in the format you want it. You can now export this into a new table for publishing

Select the plus icon and select Lakehouse, you can now select the Lakehouse you used in your previous step to save your new table that will be created from your Dataflow

Next after you have selected the Lakehouse you are exporting to give your new table a name

The next step will show you how your data has been mapped. You shouldn’t need to change any of these settings (well in my case anyway). Select Save Settings.
After you have closed this dialogue box you can then select the Publish button in the bottom right hand corner of your Dataflow. This will close the Dataflow and commence the publish process once this is complete you can go into your Lakehouse and view your published table.
Adding your Dataflow to your Pipeline
Now you can go back into your Data Pipeline and select the arrow icon on your Copy data action which will allow you to add another Action to be carried out after the Copy data action has run. In this case we select Dataflow

You can then select the Dataflow you just selected in your previous step.

You can now select the Schedule icon at the top of your screen which will enable you to select when your Data Pipeline runs and transforms your data that goes into your Lakehouse.

This is how I have been transforming data into MongoDB. If there are better ways I’d love to hear your feedback and if you have struggled with the same task and found this helpful, please let me know.