Importing a CSV File into MongoDB
home
Illustrates how to use the MongoDB connector to import data in a CSV format from a local directory into MongoDB. This examples also covers other important components in Anypoint Studio including DataMapper and Scopes such as the Message Enricher and Foreach.
Example
In this example we transform a sample CSV file containing sales data into a Map with a key value pair. We use the Datamapper transformer to do so. This Map is basically a collection called customers_Copy. We now use the MongoDB connector embedded in a Message Enricher scope to check if such a collection exists in the database.
The message enricher basically enriches the message from #[payload]
to #[flowVars['existsCollection']]
. This is then used by the choice router to decide whether to route to a MongoDB connector that creates a collection or just use the default option. The last MongoDB connector embedded in the For Each scope, saves the object from the map, iteratively for each of the elements in the collection.
Set Up and Run the Example
- Download mongoDB and install it. If you are are running a linux based OS, Homebrew is an easy way to install MongoDB.
- Run MongoDB:
- Make sure that you have created /data/db in your filesystem according to installation instructions
- Now open two instances of the command terminal. In the first window start the MongoDB server by navigating to MongoDB install path/bin and typing
mongo
. You should get the following message when your server is up and running:2017-07-01T16:15:25.282-0700 [initandlisten]
waiting for connections on port 27017
- In the second window run MongoDB by navigating to the MongoDB install path/bin and typing
mongo
. You should get the following message when you are connected.MongoDB shell version: 2.6.3 connecting to: test
- Stay in the second command terminal window and use the following commands to create a database and a user called mule who is granted permission to access the database:
use customers db.createUser ( { user: "mule", pwd: "mule", roles: [ { role: "userAdmin", db: "admin" } ] } )
- Open this example application in Studio.
- Create a folder called input under src/main/resources and copy the input.csv file into the input folder.
- Click the File Connector and edit the path to the input folder as follows:
Path = src/main/resources/input
- Run the example project as a mule application
- Go to the mongoDB console (the second command terminal window) and type:
The output then shows the inserted objects similar to what is below.db.customers.find()
> db.customers.find() { "\\_id" : ObjectId("53b2518b03643b83cf1cecde"), "firstname" : "aaa", "surname" : "vbbb", "phone" : "ccc", "state" : "sss" }