Source: Medium

MongoDB: Using Spring Data MongoDB with Oracle Database's MongoDB API

MongoDB is a popular, open-source NoSQL database management program that stores data in a flexible, JSON-like format, making it ideal for handling large, unstructured, or semi-structured data. The Oracle Database MongoDB API translates the MongoDB client requests into SQL statements that are run by Oracle Database, allowing developers to easily use MongoDB clients with Oracle Database. The Oracle Database MongoDB API works best with release 22.3 or later of Oracle REST Data Services (ORDS) In this article, we'll implement a basic MongoRepository for Oracle Database demonstrating Oracle-MongoDB wire compatibility in the OracleMongoDBTest test class. Example operations for CRUD, querying, and MongoTemplate are included - each being identical to if you were actually using MongoDB. This example requires an Oracle Database instance with ORDS installed. The easiest way to do this is to configure an Oracle Autonomous Database for JSON processing, as described in the Using Oracle Database API for MongoDB document . Note that two free, license included Autonomous Database instances are included in OCI's Always-Free tier . Once you have your database configured, save your database URI, username, and password before continuing to the next section. For a Student document , we can easily implement a Student MongoRepository : Notice how only Spring Data MongoDB APIs are used? This module only depends on spring-data-mongodb - No Oracle dependencies are on the classpath . Let's test this out in the OracleMongoDBTest , using your pre-configured database instance. You'll need to set the DB_USERNAME, DB_PASSWORD, and DB_URI environment variables to run this code. In this test, we use standard Spring repository methods to create, update, query, and delete a student document. On the Database backend, MongoDB requests are transparently converted to SQL: Let's add a few custom queries to our StudentRepository to test out the functionality with Oracle Database. Next, we can add a test case to OracleMongoDBTest that tries these custom query methods out: If you prefer to use MongoTemplate this also works with Oracle Database - We can inject MongoTemplate into our test and create another test case around this to try it: Using your pre-configured database instance, set the following environment variables: Then, you can run the test using Maven from the root of the spring-data-mongodb-oracle-api module: You should see output similar to the following. I recommend reading the test class to get an idea of what it's doing. Note that no Oracle-specific APIs are being invoked by the client! Questions? Leave a comment or drop me line on LinkedIn !

Read full article »
Annual Revenue
$1.0-5.0B
Employees
5.0-10K
Dev Ittycheria's photo - President & CEO of MongoDB

President & CEO

Dev Ittycheria

CEO Approval Rating

83/100

Read more