How to Integrate MongoDB With MuleSoft (Mule 4)
Get your NoSQL on.
Join the DZone community and get the full member experience.
Join For FreeIn this blog, we will demonstrate how to integrate MongoDB with MuleSoft and also demonstrate a few operations with simple examples, such as creating collections, inserting documents, etc.
Before getting started, make sure that you have access to a MongoDB cluster with write permissions or refer to our blog if you’d like to install MongoDB on a Windows machine.
What Is MongoDB?
MongoDB is an open-source, document-oriented database that stores data in the form of documents (key and value pairs). For more information, visit our MongoDB Hub or official MongoDB documentation.
Integrate MongoDB With MuleSoft
Follow the steps below to integrate MongoDB with MuleSoft and perform some basic MongoDB operations.
MongoDB Global Configuration
First, create a MongoDB Global configuration as shown below:
Step 1: Add the required libraries. To add the libraries, click on Configure and select Add recommended libraries.
Step 2: Once you added the required libraries, now you will need to add a connection string.
Note: we have selected the connection type Connection string. If you make the connection type Connection, then the configuration will be different.
The connection string syntax is:
mongodb://username:password@host:port/database
If you are connecting to a cluster, then the syntax is:
mongodb+srv://username:password@clusterURL/database_name
Create Collection
First, let’s create a collection called users in the vanchiv database.
To create a collection, simply drag and drop the create collection operation and configure as shown below:
Now run the application and verify whether the collection is created or not.
> show collections
users
Insert Document
Once you have created the collection, insert a document into it.
To insert documents, drag and drop the insert document and configure as shown below:
The sample input payload is:
{
"userid": "1",
"username": "NaGG" }
Now run the application and verify whether the document is inserted or not.
> db.users.find()
{ "_id" : ObjectId("6001b281b1d1732597ace274"), "userid" : "1", "username" : "NaGG" }
Great! There you can see the document has been inserted into users collection. Remember, MongoDB will create a unique id for each document "_id" : ObjectId("6001b281b1d1732597ace274")
. Read my introduction to MongoDB to learn more.
Mule also sends a response like the one below for the insert document operation.
{
"userid": "1",
"username": "NaGG",
"_id": { "$oid": "6001b281b1d1732597ace274" } }
We hope this blog helped you to integrate MongoDB with MuleSoft and perform some basic operations.
There are many ways you can use MongoDB operations. We will cover each operation in separate blogs.
Published at DZone with permission of Nagaraju Kshathriya. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments