How to Ingest Data Into MongoDB with Node.js

Now that we got data from MongoDB and loaded into Postgres with some transformation, let’s put the data back into MongoDB. This post is the continuation of the previous post about ingesting data from MongoDB. You should totally start from How to Ingest Data From MongoDB with Node.js.

The aim is simple. We are going to get the data from the two tables, restaurants and grades (which were originally created here with Python or here with Node.js), reconstruct original JSON structures and load it back to MongoDB.

The transformation is a little bit more interesting because we have to wrangle normalised tables back into the original JSON format.

Let’s start with putting the JSON back together.

joinRestaurants.js

Creating the array with grades aggregated per unique id from the tabular table format is challenging. There may be a better way than using two for loops because for loops are notoriously slow. It works at least.

To rearrange the main table, I used the node-json-transform module. For any simple JSON wrangling, I always use it because it is simple to use. I don’t think the module does the transformation required for the grades table, though.

In the end, the two JSON objects are joined by the common key (_id).

The function takes the parent and child (which means restaurants and grades) JSON object as argument parameters. We will export this module.

mongoLoad.js

This is a straight forward function that takes database name, collection name and JSON object as parameters. It truncate the collection first and load the data. The connection details are imported as in the previous post.

main.js

Now everything comes together. In the main file, it queries two tables and pass the results to the join function. Once the data is joined, it gets passed to the data load function. The db connection details are imported as in the previous post.

Data Engineering
Sending XML Payload and Converting XML Response to JSON with Python

If you need to interact with a REST endpoint that takes a XML string as a payload and returns another XML string as a response, this is the quick guide if you want to use Python. If you want to do it with Node.js, you can check out the post …

Data Engineering
Sending XML Payload and Converting XML Response to JSON with Node.js

Here is the quick Node.js example of interacting with a rest API endpoint that takes XML string as a payload and return with XML string as response. Once we get the response, we will convert it to a JSON object. For this example, we will use the old-school QAS (Quick …

Data Engineering
Downloading All Public GitHub Gist Files

I used to use plug-ins to render code blocks for this blog. Yesterday, I decided to move all the code into GitHub Gist and inject them from there. Using a WordPress plugin to render code blocks can be problematic when update happens. Plugins might not be up to date. It …