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:
- Documentation: Using Oracle Database API for MongoDB
- Documentation: Oracle Database API for MongoDB
- Video: Oracle Database API for MongoDB
Need help?
- Talk to us on Slack: #oracle-db-json
These steps show how to create an always-free Autonomous Database but any 21c or later version of Oracle Database will also work.
-
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.
-
Sign-in to the cloud console and click on Autonomous JSON Database under Oracle Database.
-
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.
-
Once the database is created, click on Service Console
Click on Development:
Copy the connection string for your database:
-
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 -
Build the examples:
mvn package
-
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 theADMIN
password you specified when you created the database -
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'
-
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'