How do I use MongoDB with my Java app?

What’s the best way of doing that on Platform?

Java is one of the most popular programming languages in the MongoDB Community. For new users, it’s essential to provide an overview of how to work with the MongoDB Java driver at Platform.sh.

Java has several frameworks and combinations to work with MongoDB, but it’s important to provide an overview of how to work with the MongoDB Java driver and how to use MongoDB as a Java developer. Therefore, the documentation provides tight integration between Java and MongoDB.

This integration became easier thanks to the Java Config Reader.

MongoDB database = config.getCredential("mongodb", MongoDB::new);
MongoClient mongoClient = database.get();
final MongoDatabase mongoDatabase = mongoClient.getDatabase(database.getDatabase());
MongoCollection<Document> collection = mongoDatabase.getCollection("scientist");
Document doc = new Document("name", "Ada Lovelace").append("city", "London");
collection.insertOne(doc);
Document myDoc = collection.find(eq("_id", doc.get("_id"))).first();

However, we have the option to overwrite the configurations only when it is running at Platform.sh such as Spring Boot MongoDB and Jakarta EE NoSQL.

Articles

More Java combinations

We have several articles and source code on this repository:

This repository has several Java combinations it includes Spring, Jakarta EE, MicroProfile, MongoDB, JPA, and so on.