diff --git a/persistence-modules/java-mongodb/pom.xml b/persistence-modules/java-mongodb/pom.xml index 9658ef567f..0f5efedb96 100644 --- a/persistence-modules/java-mongodb/pom.xml +++ b/persistence-modules/java-mongodb/pom.xml @@ -35,8 +35,8 @@ 1.8 1.8 - 3.4.1 + 3.10.1 1.11 - \ No newline at end of file + diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/MongoBsonExample.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/MongoBsonExample.java new file mode 100644 index 0000000000..0ad3dfae30 --- /dev/null +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/MongoBsonExample.java @@ -0,0 +1,79 @@ +package com.baeldung; + +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import org.bson.Document; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class MongoBsonExample +{ + public static void main(String[] args) + { + // + // 4.1 Connect to cluster (default is localhost:27017) + // + + MongoClient mongoClient = MongoClients.create(); + MongoDatabase database = mongoClient.getDatabase("myDB"); + MongoCollection collection = database.getCollection("employees"); + + // + // 4.2 Insert new document + // + + Document employee = new Document() + .append("first_name", "Joe") + .append("last_name", "Smith") + .append("title", "Java Developer") + .append("years_of_service", 3) + .append("skills", Arrays.asList("java", "spring", "mongodb")) + .append("manager", new Document() + .append("first_name", "Sally") + .append("last_name", "Johanson")); + collection.insertOne(employee); + + // + // 4.3 Find documents + // + + + Document query = new Document("last_name", "Smith"); + List results = new ArrayList<>(); + collection.find(query).into(results); + + query = + new Document("$or", Arrays.asList( + new Document("last_name", "Smith"), + new Document("first_name", "Joe"))); + results = new ArrayList<>(); + collection.find(query).into(results); + + // + // 4.4 Update document + // + + query = new Document( + "skills", + new Document( + "$elemMatch", + new Document("$eq", "spring"))); + Document update = new Document( + "$push", + new Document("skills", "security")); + collection.updateMany(query, update); + + // + // 4.5 Delete documents + // + + query = new Document( + "years_of_service", + new Document("$lt", 0)); + collection.deleteMany(query); + } +}