How to call MongoDB JavaScript using SSIS

Introduction

SSIS PowerPack comes with great features for MongoDB Integration Scenarios but in this post we will focus on how to call MongoDB Javascript (Server side) using SSIS. For this purpose we will use SSIS MongoDB Execute SQL Task

This powerful task not only easy to use but it supports calling any valid MongoDB commands including Server side Java Script.

NOTE: ZappySys could not support $eval function, If you’ve installed MongoDB version higher than 3.0 version as MongoDB have removed support for the $eval function from version 4.2.
If you still want to use $eval function then you need to downgrade MongoDB to a lower than 4.2 version.
Please Refer to MongoDB Release Notes for reference.

How to call MongoDB JavaScript – Example

To call various MongoDB shell commands or execute JavaScript in MongoDB using SSIS you have Download and Install SSIS PowerPack . Once its installed perform following steps.

  1. From SSIS Toolbox drag ZS MongoDB ExecuteSQL Task
  2. Click New to create new MongoDB connection manager
  3. Once connection is configured click Test and OK to save
  4. Now you can use {{Examples}} dropdown to see possible scenarios with MongoDB ExecuteSQL task
  5. Below is sample script to call Aggregate command

 NOTE: See how we returned command output using toArray() Javascript function at the end? This will give us all records in clean JSON Array format which can be consumed easily using SSIS JSON Source. If you don’t use toArray then you may get Cursor with only first batch.

ZappySys MongoDB Shell command syntax (i.e. JSON) supports both double quotes and single quotes around the property name or value (i.e. your script). If you wrap your code with single quote then do not use single quote inside script block.

Call MongoDB single line commands

If you wish to call commands which are single line only then you may use below code which is simpler than previous example. Notice it doesn’t require function() keyword like previous one. Make sure your code has to be in single line or try to use \r\n for new line

Permissions needed to call JavaScript

Calling eval command requires special permission so make sure you setup permission so MongoDB user can call db.runCommand ( {eval command} ). Here is more information about calling eval command in MongoDB.

Save Result JSON of MongoDB command into SSIS variable

If you want to save MongoDB command output as JSON into SSIS Variable then set Result mode = RawResult

Call MongoDB JavaScript in SSIS Using Execute SQL Task - Aggregate Command Example - Save raw JSON output into SSIS variable

Call MongoDB JavaScript in SSIS Using Execute SQL Task – Aggregate Command Example – Save raw JSON output into SSIS variable

How to use MongoDB Eval command to call runCommand function

Now lets look at how to call some admin functions using runCommand function. In below example we setting Global Timeout setting for Cursor (This helps to avoid timeout for long running cursor query)

Setting MongoDB Cursor Timeout ( set cursorTimeoutMillis parameter )

Setting MongoDB Cursor Timeout ( set cursorTimeoutMillis parameter )

 

Save Result of MongoDB command into SSIS variable as ADO.net Table

If you want to use ForEach Loop to iterate through certain records from MongoDB result then set Result mode = FullResult this will save result as ADO.net table rather than JSON. Example : Get all collections and loop through them in SSIS using ForEachLoop Task?

Call MongoDB JavaScript in SSIS - Aggregate Command - Save JSON output as ADO.net Table - Loop Through ForEachLoop

Call MongoDB JavaScript in SSIS – Aggregate Command – Save JSON output as ADO.net Table – Loop Through ForEachLoop

Conclusion

MongoDB is the most popular NoSQL Database engine but integrating that into SSIS can be challenging without right tools. ZappySys SSIS PowerPack comes with many MongoDB Tasks/Components for SSIS.

Posted in SSIS MongoDB ExecuteSQL and tagged , , , , , , .