[UiPath] + MongoDB howto: connection, read collections, update fields (with example workflows)

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! :slight_smile: 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 :arrow_double_down: download at the end of this article.

Introduction

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.

In this article, I will show the current possibilities for integration between MongoDB and UiPath.

UiPath activities for Mongo DB

There is no official package from UiPath. At UiPath Marketplace you can find 2 bronze-certified packages:

  1. 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.
  2. 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:

Mongo Activities for UiPath (by Shivam Kaushik)

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):

  • MongoDB.Bson.BsonDocument
    • 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().
  • MongoDB.Driver.IMongoCollection<MongoDB.Bson.BsonDocument>
    • 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).

Activities:

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.
image

:one: Connect to Mongo

image 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 :slight_smile:

:two: Get Mongo Collection

image Provide DataBase (from previous activity) and CollectionName = name of target collection.
As a result, you will receive
MongoDB.Driver.IMongoCollection
<MongoDB.Bson.BsonDocument> variable.
Again, use CTRL+K to initiate new variable :male_detective:

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:

  1. Iterate (loop) through the collection and read content of the fields or
  2. Filter the collection = narrow down the collection by some filtering logic.

It is both conveniently possible through the next activity:

:three: Find Activity

image
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:

image

If you only wish to loop through the original collection without filtering enter to FilterQuery this:
new BsonDocument()

The nice thing about this activity is that it outputs FindResults as data type System.Collections.Generic.List<MongoDB.Bson.BsonDocument>.

:spiral_notepad: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
resultOfFind(0)("_id") 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.
resultOfFind(0) 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.
resultOfFind.Last("name") returns value of field named “name” from last entry from collection
resultOfFind.Count 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.

:four: Delete Many

image 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.

:five: Insert One

image 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:

How to :pencil2: update/edit some field in MongoDB?

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).

How does it work? :thinking:

This setup will update the field name with new value: “New value” in collection called “myCollection” in database called “myDatabase” where field id = “87”
image

This example code works with String variables. You can adjust it for other use, as well as the C# code inside. :slight_smile:

:arrow_double_down: Download Example workflows

MongoExamples.zip (59.5 KB)

Troubleshooting

Are you getting some strange errors about Builders and so?
Make sure you have exactly these namespaces imported:
image
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!)

1 Like

Finally a good and detailed guide how to use MongoDB in UiPath. Once you need to use these two in real world, not just check it theoretically, you will find out there is nothing really out there you could lean on. THIS is just what I needed. Thank you again!

1 Like