Welcome to this Tigris MongoDB compatibility and Java examples repo. This repo aims to show a basic examples of how you can use the power of Tigris MongoDB compatibility with Java.
This project uses MongoDB Java driver.
Tigris MongoDB compatibility supports the MongoDB 6.0+ wire protocol, so any drivers or other components must support this version.
- Java JDK 8 to 15.
- Maven 3.6.3.
- A Tigris Cloud account or you can self-host Tigris
- Create a project in Tigris.
- Create an application key, and copy the Project Name, Client ID, and Client Secret values.
Before running the examples, export
the Tigris connection string and a database name:
export TIGRIS_CONNECTION_STRING="mongodb://{TIGRIS_CLIENT_ID}:{TIGRIS_CLIENT_SECRET}@m1k.preview.tigrisdata.cloud:27018/?authMechanism=PLAIN&tls=true"
export TIGRIS_PROJECT_NAME="{TIGRIS_PROJECT_NAME}"
Replacing {TIGRIS_CLIENT_ID}
with your Tigris Client ID, {TIGRIS_CLIENT_SECRET}
with your Tigris Client Secret,
and {TIGRIS_PROJECT_NAME}
with the name of your Tigris Project. Remember to quote the values.
Next, compile:
mvn clean compile
Test the HelloMongoDB
hello world example:
mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.HelloMongoDB" -Dexec.cleanupDaemonThreads=false
Now you're ready to run the examples:
Run the Connection
class:
mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.Connection" -Dmongodb.uri="$TIGRIS_CONNECTION_STRING" -Dexec.cleanupDaemonThreads=false
The MappingPOJO
class show full CRUD functionality using a schema defined via a Java class:
mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.MappingPOJO" -Dmongodb.uri="$TIGRIS_CONNECTION_STRING" -Ddb.name="$TIGRIS_PROJECT_NAME" -Dexec.cleanupDaemonThreads=false
Other examples using a dynamic schema can be found below:
Please note: You may need to delete the grades
collection from the Tigris web console before running the follow examples.
Run the Create
class:
mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.Create" -Dmongodb.uri="$TIGRIS_CONNECTION_STRING" -Ddb.name="$TIGRIS_PROJECT_NAME" -Dexec.cleanupDaemonThreads=false
- Run the
Read
class:
mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.Read" -Dmongodb.uri="$TIGRIS_CONNECTION_STRING" -Ddb.name="$TIGRIS_PROJECT_NAME" -Dexec.cleanupDaemonThreads=false
Run the Update
class:
mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.Update" -Dmongodb.uri="$TIGRIS_CONNECTION_STRING" -Ddb.name="$TIGRIS_PROJECT_NAME" -Dexec.cleanupDaemonThreads=false
Run the Delete
class:
mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.Delete" -Dmongodb.uri="$TIGRIS_CONNECTION_STRING" -Ddb.name="$TIGRIS_PROJECT_NAME" -Dexec.cleanupDaemonThreads=false
Tigris presently has limited support for Aggregation.
Run the AggregationFramework
class:
mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.AggregationFramework" -Dmongodb.uri="$TIGRIS_CONNECTION_STRING" -Ddb.name="$TIGRIS_PROJECT_NAME" -Dexec.cleanupDaemonThreads=false
- Run the
ChangeStreams
class:
mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.ChangeStreams" -Dmongodb.uri="$TIGRIS_CONNECTION_STRING" -Ddb.name="$TIGRIS_PROJECT_NAME" -Dexec.cleanupDaemonThreads=false
Run the ClientSideFieldLevelEncryption
class:
mvn compile exec:java -Dexec.mainClass="com.mongodb.quickstart.csfle.ClientSideFieldLevelEncryption" -Dmongodb.uri="$TIGRIS_CONNECTION_STRING" -Ddb.name="$TIGRIS_PROJECT_NAME" -Dexec.cleanupDaemonThreads=false
- Find out more about Tigris MongoDB compatibility
- Join the Tigris Discord
Additional MongoDB resources:
- MongoDB & Java - CRUD Operations Tutorial
- Java - Mapping POJOs
- Java - Aggregation Pipeline
- Java - Change Streams
- Java - Client Side Field Level Encryption
The original repository author: Maxime Beugnet maxime@mongodb.com