Create a Data Access Object for MongoDB Data using JDBI



A brief overview of creating a SQL Object API for MongoDB data in JDBI.

JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for MongoDB integrates connectivity to live MongoDB data in Java applications. By pairing these technologies, you gain simple, programmatic access to MongoDB data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write MongoDB data.

About MongoDB Data Integration

Accessing and integrating live data from MongoDB has never been easier with CData. Customers rely on CData connectivity to:

MongoDB's flexibility means that it can be used as a transactional, operational, or analytical database. That means CData customers use our solutions to integrate their business data with MongoDB or integrate their MongoDB data with their data warehouse (or both). Customers also leverage our live connectivity options to analyze and report on MongoDB directly from their preferred tools, like Power BI and Tableau.

For more details on MongoDB use case and how CData enhances your MongoDB experience, check out our blog post: The Top 10 Real-World MongoDB Use Cases You Should Know in 2024.


Getting Started


Create a DAO for the MongoDB restaurants Entity

The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.

public interface MyrestaurantsDAO { //insert new data into MongoDB @SqlUpdate("INSERT INTO restaurants (Name, cuisine) values (:name, :cuisine)") void insert(@Bind("name") String name, @Bind("cuisine") String cuisine); //request specific data from MongoDB (String type is used for simplicity) @SqlQuery("SELECT cuisine FROM restaurants WHERE Name = :name") String findcuisineByName(@Bind("name") String name); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to MongoDB

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to MongoDB.

Set the Server, Database, User, and Password connection properties to connect to MongoDB. To access MongoDB collections as tables you can use automatic schema discovery or write your own schema definitions. Schemas are defined in .rsd files, which have a simple format. You can also execute free-form queries that are not tied to the schema.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the MongoDB JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.mongodb.jar

Fill in the connection properties and copy the connection string to the clipboard.

A connection string for MongoDB will typically look like the following:

jdbc:mongodb:Server=MyServer;Port=27017;Database=test;User=test;Password=Password;

Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.

DBI dbi = new DBI("jdbc:mongodb:Server=MyServer;Port=27017;Database=test;User=test;Password=Password;"); MyrestaurantsDAO dao = dbi.open(MyrestaurantsDAO.class); //do stuff with the DAO dao.close();

Read MongoDB Data

With the connection open to MongoDB, simply call the previously defined method to retrieve data from the restaurants entity in MongoDB.

//disply the result of our 'find' method String cuisine = dao.findcuisineByName("Morris Park Bake Shop"); System.out.println(cuisine);

Write MongoDB Data

It is also simple to write data to MongoDB, using the previously defined method.

//add a new entry to the restaurants entity dao.insert(newName, newcuisine);

Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for MongoDB by integrating with the CData JDBC Driver for MongoDB. Download a free trial and work with live MongoDB data in custom Java applications today.

Ready to get started?

Download a free trial of the MongoDB Driver to get started:

 Download Now

Learn more:

MongoDB Icon MongoDB JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with MongoDB document databases.