Oracle Blog

Inside Oracle's Data Integration community

Tuesday Jul 09, 2013

In this post I will show how to load documents into a MongoDB collection. The interface design looks just like all other ODI interfaces, but behind the scenes the KM configured in the physical design uses the MongoDB SDK (see MongoDB SDK here) to insert the documents. The target datastore below represents a MongoDB document, the columns are the keys in the document. Each row is inserted as a document, and each column is a key, the column value is the value. The ENAME value below is shown as a complex JSON value.

The IKM I have used is a multi-connect IKM, the source is a SQL data source and the target uses a MongoDB groovy command. The heart of the IKM to insert the documents into the collection has a SQL select as the source command and the following groovy code for the target command;

The odiRef.getColList method call above generates code for every target column, the code performs a doc.put invocation to add the key-value pairs into the document. For example this is the code generated and executed based on the interface design discussed above;

import com.mongodb.*

MongoClient mongoClient = new MongoClient("DALLAN-SVR", 27017);

DB db = mongoClient.getDB("test");

DBCollection coll = db.getCollection("testCollection")

BasicDBObject doc = new BasicDBObject();

doc.put("EMPNO", "#EMPNO");

doc.put("ENAME", "#ENAME");

doc.put("JOB", "#JOB");

doc.put("MGR", "#MGR");

doc.put("HIREDATE", "#HIREDATE");

doc.put("SAL", "#SAL");

doc.put("COMM", "#COMM");

doc.put("DEPTNO", "#DEPTNO");

coll.insert(doc);

This is a simple illustration of how to load documents into MongoDB. We can go into the MongoDB command line and execute the command to see all objects in the collection and get the list of documents, below you can see a preview of executing db.testCollection.find()

You can see the key:value pairs in our document. For those MongoDB gurus, you'll notice in the 'complex' data illustration, this is really a string and not a MongoDB complex object - that discussion is for another day.

This post is not just about MongoDB, but also a useful post on how to integrate APIs as a target in a data flow.