Hi! Here is a video based on this tutorial.
So you can watch it, or you can read about it in this tutorial! Or you can do both! Please let me know if you have any questions and if something would be not understandable. This is a long article covering a lot of information, so I might miss something.
You can find example workflows to download at the end of this article.
In this article, I will show the current possibilities for integration between MongoDB and UiPath.
There is no official package from UiPath. At UiPath Marketplace you can find 2 bronze-certified packages:
MongoDB - Collection Data to DataTable - RPA Component | UiPath Marketplace
This package by binamra barik provides easy way how to retrieve a collection as a DataTable. It workes as expected, however in case your collection containes nested data/more than one level, it will retrieve the field as JSON-like format. For simple collections with only one layer of fields it is a neat activity.
Mongo Activities for UiPath - RPA Component | UiPath Marketplace
This package by shivam kaushik provides more functionality. They provide quite some tools, so let’s explore them further:
PLEASE carefully follow Installation Guide that can be found on UiPath Marketplace: InstallationGuide.docx (587.5 KB) (from: Mongo Activities for UiPath - RPA Component | UiPath Marketplace )
UiPath is a low-code platform automation tool. Activities are sometimes just a nice “field” wrappers that actually execute some not that complicated code (in Visual Basic/C#). This valuable package allows even those without much coding experience to connect to MongoDB, retrieve some data, perform filter and manipulate the collections - all this from UiPath.
MongoDB uses specific data types, that might be confusing for beginners, so let me do just a brief introduction (you may need to study a bit about some other types):
- is a JSON-like formatted data type.
- You can get data from each fields (search for how to get data from JSON if you are unfamiliar with this).
- You can “see” what is inside (display the JSON tree) by familiar method .ToString().
- is a collection of BsonDocuments and you can perform filtering on it which will allow you to iterate through the collection (I will show you how to do it).
There are 5 activities that are covering basic operations within MongoDB. Unfortunately, author did not add some kind of Update in DB activity, neither Insert to DB. So we will need to find a workaround for this.
You can see some abstraction on how to work with these activities in relation of how variables are being passed between them. Of course, other combinations are also possible.
|Provide ConnectionString to your MongoDB and DatabaseName.
As a result, you will receive MongoDB.Driver.MongoDatabaseBase variable.
Click CTRL+K to create it as new variable
|Provide DataBase (from previous activity) and CollectionName = name of target collection.
As a result, you will receive
Again, use CTRL+K to initiate new variable
So far so good. Run it and if there is no error, you established the connection and you most probably are able to get some data from your existing collection. Next you may wish to do one of these steps:
- Iterate (loop) through the collection and read content of the fields or
- Filter the collection = narrow down the collection by some filtering logic.
It is both conveniently possible through the next activity:
The name is confusing. It would be better without “Activity”, since it seems as we are trying to find some “Activity” which we are actually not.
We have to provide to this activity Collection (we got it from the previous step) and FilterQuery which takes BsonDocument variable. If you start typing new BsonDocument( you will see list of hints which formats (and data types) can be provided to construct a new Bson Document. It can be for example dictionary. Or for a simple filtering based on one field → two parameters: 1st: field name, 2nd: field value. See example from author below:
If you only wish to loop through the original collection without filtering enter to FilterQuery this:
The nice thing about this activity is that it outputs FindResults as data type System.Collections.Generic.List<MongoDB.Bson.BsonDocument>.
List is a commonly used data type and you can easily access what is in the list and also you can easily loop through the list.
|example expression||what it does|
||returns value of field named “id” from 1st entry from collection.
Carefully! Result may be various data type!
Perform .ToString() to convert anything to string.
||returns 1st entry from collection - it will be data type BsonDocument, we can store it in some variable to later access the entry’s fields.|
||returns value of field named “name” from last entry from collection|
||returns number of entries in the result list (as Int32)|
You can loop through the fields using a For each activity - you have to specify property “TypeArgument” as MongoDB.Bson.BsonDocument. Then you can access field of each entry inside the loop.
|You have to supply it with Collection - for example from Get Mongo Collection activity.
Specify FilterQuery with similar logic as in previous activity.
Supplied collection will be altered in the database.
Entries that fall under the criterio of the FilterQuery will be deleted.
|This activity will insert new entry to the database.
It will insert a BsonDocument inside a collection. Structure of entries within collection can be different. Test it properly!
So now we can read, delete and add. But how do we perform update of some field? Let’s see:
Since updating a field is not possible via the activities from MongoDB.Activities package, I went with creating a workflow that utilize Invoke Code. In the Invoke Code there is a C# script to connect to db and a function to update a field. It is an example of how it can be done, it is only working with fields with data type of string and it is not bulletproof. Also I am not really great at C#. So you are welcome to adjust it to your needs. You can download this snippet and try it (link below).
This setup will update the field name with new value: “New value” in collection called “myCollection” in database called “myDatabase” where field id = “87”
This example code works with String variables. You can adjust it for other use, as well as the C# code inside.
MongoExamples.zip (59.5 KB)
Are you getting some strange errors about Builders and so?
Make sure you have exactly these namespaces imported:
Make also sure you have MongoDB.Driver package ready within your project dependencies.
(Follow correctly all steps again. If you are still in doubts, comment in this thread or in Ask section! I will try to help you!)