Configure MongoDB connector

Apisero
4 min readJul 1, 2021

Author: Aditya Veer

MongoDB connector is used to perform operations on the NoSQL database. It is a connector that interconnects MongoDB, a third-party software with the Mule. MongoDB is a NoSQL database, The data stored in the collection has a changeable schema

Add a MongoDB connector in Anypoint studio

  • First, you have to go on your ‘ Mule Palette ’. Then you have to select ‘ Search in Exchange ’ (Here you can add the dependencies by searching them in Exchange)
  • Add your Anypoint Platform account
  • Search for the MongoDB connector — Mule 4, Published by ‘ Mulesoft ’
  • Select it and Add it
  • Click on ‘Finish’. The connector will be added in the ‘ Mule Palette ’

Configure your MongoDB connector

  • Move to ‘ Global Elements ’ and click on ‘ Create ’
  • Expand the ‘ Connector Configuration ’ and there you can see the ‘ MongoDB Config ‘, open it

<dependency>

<groupId>org.mongodb</groupId>

<artifactId>mongodb-driver-legacy</artifactId>

<version>4.0.4</version>

<type>jar</type>

</dependency>

  • Add this dependency in the ’ Required Libraries ‘
  • Change the ‘ Servers (host: port) ’ to ‘ Edit inline ‘
  • Click on the ‘ + ’ symbol and add host and port also add the Database on which you want to work
  • Now click on ‘Test Connections’ you will get a success

Goal = To upload a CSV file in NoSQL database and learn the use of MongoDB connector

MongoDB connectors required in this POC

  • Collection exists
  • Create collection
  • Insert documents
  • Add a connector named ‘On New or Updated File ’ configure it so that you can send a CSV file through it
  • Click on Advance in ‘On New or Updated File ’ and define the metadata for the CSV and Select ‘ MIME ’ type as ‘ application/CSV ’
  • Then add a ‘ Transform Message ’
  • In here convert the payload into JSON format
  • Add a logger and log the payload
  • Drag and drop the component ‘ Collection exists ‘
  • Add the ‘ connector configuration ‘ and select the collection on which you want to add the data
  • Now click on the advance
  • Add a target variable name now this target variable will point to the payload which you are getting
  • Add a ‘ choice ‘ router
  • Add ‘ not vars.<Your target variable name> ‘ in the first router of ‘ choice ‘ and keep the default one empty
  • In the first router of ‘ choice ‘ add ‘ Create collection ’ connector
  • ‘ Create collection ’ connector will create a collection
  • What we are doing here is that we are seeing if the collection previously exists in the database then no need to create a new collection. If it is not present then first create it and then move on
  • Drag and drop ‘ Insert documents ’ connector
  • Add the collection name in the ‘ Insert documents ’ connector
  • Add a ‘ Transform ’ connector convert data into JSON and map the payload
  • Add a ‘ logger ’ and pass the payload
  • Run the flow and you can see the data of CSV file inserted into the database

--

--