diff --git a/persistence-modules/java-mongodb-3/src/main/java/com/baeldung/mongo/insert/InsertArrayOperation.java b/persistence-modules/java-mongodb-3/src/main/java/com/baeldung/mongo/insert/InsertArrayOperation.java new file mode 100644 index 0000000000..e073a1ec88 --- /dev/null +++ b/persistence-modules/java-mongodb-3/src/main/java/com/baeldung/mongo/insert/InsertArrayOperation.java @@ -0,0 +1,157 @@ +package com.baeldung.mongo.insert; + +import com.mongodb.*; +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; +import com.mongodb.client.MongoDatabase; +import org.bson.Document; +import org.bson.conversions.Bson; + +import java.util.ArrayList; +import java.util.List; + +import static com.mongodb.client.model.Filters.eq; +import static com.mongodb.client.model.Filters.in; + +public class InsertArrayOperation { + + private static MongoClient mongoClient; + private static MongoDatabase database; + private static MongoCollection collection; + private static DB db; + private static DBCollection dbCollection; + private static String collectionName; + private static String databaseName; + + public static void setUp() { + if (mongoClient == null) { + mongoClient = new MongoClient("localhost", 27017); + + databaseName = "baeldung"; + collectionName = "student"; + + database = mongoClient.getDatabase(databaseName); + collection = database.getCollection(collectionName); + + db = mongoClient.getDB(databaseName); + dbCollection = db.getCollection(collectionName); + } + } + + public static void insertSingleBsonDocumentWithStringArray() { + BasicDBList coursesList = new BasicDBList(); + coursesList.add("Chemistry"); + coursesList.add("Science"); + + DBObject student = new BasicDBObject().append("studentId", "STUD1") + .append("name", "Jim") + .append("age", 13) + .append("courses", coursesList); + + dbCollection.insert(student); + + Bson filter = eq("studentId", "STUD1"); + FindIterable documents = collection.find(filter); + + MongoCursor cursor = documents.iterator(); + while (cursor.hasNext()) { + System.out.println(cursor.next()); + } + } + + public static void insertSingleDocumentWithStringArray() { + List coursesList = new ArrayList<>(); + coursesList.add("Science"); + coursesList.add("Geography"); + + Document student = new Document().append("studentId", "STUD2") + .append("name", "Sam") + .append("age", 13) + .append("courses", coursesList); + + collection.insertOne(student); + + Bson filter = eq("studentId", "STUD2"); + FindIterable documents = collection.find(filter); + + MongoCursor cursor = documents.iterator(); + while (cursor.hasNext()) { + System.out.println(cursor.next()); + } + } + + public static void insertMultipleDocumentsWithStringArray() { + List coursesList1 = new ArrayList<>(); + coursesList1.add("Chemistry"); + coursesList1.add("Geography"); + + Document student1 = new Document().append("studentId", "STUD3") + .append("name", "Sarah") + .append("age", 12) + .append("courses", coursesList1); + + List coursesList2 = new ArrayList<>(); + coursesList2.add("Maths"); + coursesList2.add("History"); + + Document student2 = new Document().append("studentId", "STUD4") + .append("name", "Tom") + .append("age", 13) + .append("courses", coursesList2); + + List students = new ArrayList<>(); + students.add(student1); + students.add(student2); + + collection.insertMany(students); + + Bson filter = in("studentId", "STUD3", "STUD4"); + FindIterable documents = collection.find(filter); + + MongoCursor cursor = documents.iterator(); + while (cursor.hasNext()) { + System.out.println(cursor.next()); + } + } + + public static void insertSingleDocumentWithObjectArray() { + Document course1 = new Document().append("name", "C1") + .append("points", 5); + + Document course2 = new Document().append("name", "C2") + .append("points", 7); + + List coursesList = new ArrayList<>(); + coursesList.add(course1); + coursesList.add(course2); + + Document student = new Document().append("studentId", "STUD5") + .append("name", "Avin") + .append("age", 13) + .append("courses", coursesList); + + collection.insertOne(student); + + Bson filter = eq("studentId", "STUD5"); + FindIterable documents = collection.find(filter); + + MongoCursor cursor = documents.iterator(); + while (cursor.hasNext()) { + System.out.println(cursor.next()); + } + } + + public static void main(String args[]) { + + setUp(); + + insertSingleBsonDocumentWithStringArray(); + + insertSingleDocumentWithStringArray(); + + insertMultipleDocumentsWithStringArray(); + + insertSingleDocumentWithObjectArray(); + } +} diff --git a/persistence-modules/java-mongodb-3/src/test/java/com/baeldung/mongo/insert/InsertArrayOperationLiveTest.java b/persistence-modules/java-mongodb-3/src/test/java/com/baeldung/mongo/insert/InsertArrayOperationLiveTest.java new file mode 100644 index 0000000000..a844a003f0 --- /dev/null +++ b/persistence-modules/java-mongodb-3/src/test/java/com/baeldung/mongo/insert/InsertArrayOperationLiveTest.java @@ -0,0 +1,150 @@ +package com.baeldung.mongo.insert; + +import com.mongodb.*; +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; +import com.mongodb.client.MongoDatabase; +import org.bson.Document; +import org.bson.conversions.Bson; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static com.mongodb.client.model.Filters.eq; +import static com.mongodb.client.model.Filters.in; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class InsertArrayOperationLiveTest { + + private static MongoClient mongoClient; + private static MongoDatabase database; + private static MongoCollection collection; + private static DB db; + private static DBCollection dbCollection; + + @BeforeClass + public static void setUp() { + if (mongoClient == null) { + mongoClient = new MongoClient("localhost", 27017); + + database = mongoClient.getDatabase("baeldung"); + collection = database.getCollection("student"); + + db = mongoClient.getDB("baeldung"); + dbCollection = db.getCollection("student"); + + collection.drop(); + } + } + + @Test + public void givenSingleStudentObjectWithStringArray_whenInsertingDBObject_thenCheckingForDocument() { + BasicDBList coursesList = new BasicDBList(); + coursesList.add("Chemistry"); + coursesList.add("Science"); + + DBObject student = new BasicDBObject().append("studentId", "STUD1") + .append("name", "Jim") + .append("age", 13) + .append("courses", coursesList); + + dbCollection.insert(student); + + Bson filter = eq("studentId", "STUD2"); + FindIterable documents = collection.find(filter); + + MongoCursor cursor = documents.iterator(); + + assertNotNull(cursor); + assertTrue(cursor.hasNext()); + } + + @Test + public void givenSingleStudentObjectWithStringArray_whenInsertingDocument_thenCheckingForDocument() { + List coursesList = new ArrayList<>(); + coursesList.add("Science"); + coursesList.add("Geography"); + + Document student = new Document().append("studentId", "STUD2") + .append("name", "Sam") + .append("age", 13) + .append("courses", coursesList); + + collection.insertOne(student); + + Bson filter = eq("studentId", "STUD2"); + FindIterable documents = collection.find(filter); + + MongoCursor cursor = documents.iterator(); + + assertNotNull(cursor); + assertTrue(cursor.hasNext()); + } + + @Test + public void givenMultipleStudentObjectsWithStringArray_whenInsertingDocuments_thenCheckingForDocuments() { + List coursesList1 = new ArrayList<>(); + coursesList1.add("Chemistry"); + coursesList1.add("Geography"); + + Document student1 = new Document().append("studentId", "STUD3") + .append("name", "Sarah") + .append("age", 12) + .append("courses", coursesList1); + + List coursesList2 = new ArrayList<>(); + coursesList2.add("Maths"); + coursesList2.add("History"); + + Document student2 = new Document().append("studentId", "STUD4") + .append("name", "Tom") + .append("age", 13) + .append("courses", coursesList2); + + List students = new ArrayList<>(); + students.add(student1); + students.add(student2); + + collection.insertMany(students); + + Bson filter = in("studentId", "STUD3", "STUD4"); + FindIterable documents = collection.find(filter); + + MongoCursor cursor = documents.iterator(); + + assertNotNull(cursor); + assertTrue(cursor.hasNext()); + } + + @Test + public void givenSingleStudentObjectWithObjectArray_whenInsertingDocument_thenCheckingForDocument() { + Document course1 = new Document().append("name", "C1") + .append("points", 5); + + Document course2 = new Document().append("name", "C2") + .append("points", 7); + + List coursesList = new ArrayList<>(); + coursesList.add(course1); + coursesList.add(course2); + + Document student = new Document().append("studentId", "STUD5") + .append("name", "Sam") + .append("age", 13) + .append("courses", coursesList); + + collection.insertOne(student); + + Bson filter = eq("studentId", "STUD5"); + FindIterable documents = collection.find(filter); + + MongoCursor cursor = documents.iterator(); + + assertNotNull(cursor); + assertTrue(cursor.hasNext()); + } +}