Skip to content

Files

Latest commit

 

History

History

MongoExamples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Oracle API for MongoDB - Java Examples

This directory contains examples of how to store and access JSON collections from a Java program using a MongoDB client.

  • emp.CreateCollection - Creates the employee collection employees used by all the examples.
  • emp.Insert - Inserts three JSON values into the employees collection.
  • emp.GetAll - Gets all the JSON values from the employees collection.
  • emp.Filter - Selects employees from the employees collection where the salary attribute is greater than 30,000.
  • emp.Update - Updates an employee document using whole document replacement.
  • emp.RunAll - Runs all the examples at once.
  • emp.DropCollection - Drops the collection used by the examples.

See also:

Need help?

Running the examples

Create a database

These steps show how to create an always-free Autonomous Database but any 21c or later version of Oracle Database will also work.

  1. Create a free cloud account:
    https://www.oracle.com/cloud/free/.

    It will ask for a credit card for identification purposes. Your card will not be charged unless you manually choose to upgrade out of the free-tier limits.

  2. Sign-in to the cloud console and click on Autonomous JSON Database under Oracle Database.

  3. Click Create Autonomous Database. When creating the database, ensure that workload type Transaction Processing or JSON is selected and Always Free is selected.

    You must define network access control in order to use the Oracle API for MongoDB. Add your current IP address.

  4. Once the database is created, click on Service Console

    Click on Development:

    Copy the connection string for your database:

Setup the examples

  1. Clone these examples from github. For example:

    git clone https://github.com/oracle/json-in-db.git
    cd json-in-db/MongoExamples/
    

    If you don't have git you can alternatively download them here: https://github.com/oracle/json-in-db/archive/master.zip

  2. Install Java and Maven

  3. Build the examples:

    mvn package
    

Run the examples

  1. Run all the examples using the connection string from above. Set the username to "admin" and the database to "admin":

     mvn -q exec:java \
      -Dexec.mainClass="emp.RunAll" \
      -Dexec.args='mongodb://admin:mypassword@TR8SOWHVOZCU675-DEMO.adb.us-ashburn-1.oraclecloudapps.com:27017/admin?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true'
    

    But replace mypassword with the ADMIN password you specified when you created the database

  2. Drop the collection used by the examples:

     mvn -q exec:java \
      -Dexec.mainClass="emp.DropCollection" \
      -Dexec.args='mongodb://admin:mypassword@TR8SOWHVOZCU675-DEMO.adb.us-ashburn-1.oraclecloudapps.com:27017/admin?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true'
    
  3. You can also run specific examples, one at a time:

     mvn -q exec:java \
      -Dexec.mainClass="emp.CreateCollection" \
      -Dexec.args='mongodb://admin:mypassword@TR8SOWHVOZCU675-DEMO.adb.us-ashburn-1.oraclecloudapps.com:27017/admin?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true'