BAEL-5368 Guide to Find in MongoDB (#12361)
* BAEL-5368 | Article code * BAEL-5368 | Package renamed
This commit is contained in:
parent
3624a450ed
commit
413ee370c2
5
persistence-modules/java-mongodb-3/.gitignore
vendored
Normal file
5
persistence-modules/java-mongodb-3/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
.classpath
|
||||
.project
|
||||
.settings
|
||||
target
|
||||
build
|
28
persistence-modules/java-mongodb-3/pom.xml
Normal file
28
persistence-modules/java-mongodb-3/pom.xml
Normal file
@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>java-mongodb-3</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>java-mongodb-3</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
<version>${mongo.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<mongo.version>3.12.1</mongo.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,86 @@
|
||||
package com.baeldung.mongo.find;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
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 static com.mongodb.client.model.Filters.eq;
|
||||
import static com.mongodb.client.model.Projections.fields;
|
||||
import static com.mongodb.client.model.Projections.include;
|
||||
|
||||
public class FindOperation {
|
||||
|
||||
private static MongoClient mongoClient;
|
||||
private static MongoDatabase database;
|
||||
private static MongoCollection<Document> collection;
|
||||
private static String collectionName;
|
||||
private static String databaseName;
|
||||
|
||||
public static void setUp() {
|
||||
if (mongoClient == null) {
|
||||
mongoClient = new MongoClient("localhost", 27017);
|
||||
|
||||
databaseName = "baeldung";
|
||||
collectionName = "employee";
|
||||
|
||||
database = mongoClient.getDatabase(databaseName);
|
||||
collection = database.getCollection(collectionName);
|
||||
}
|
||||
}
|
||||
|
||||
public static void retrieveAllDocumentsUsingFind() {
|
||||
FindIterable<Document> documents = collection.find();
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
}
|
||||
|
||||
public static void retrieveAllDocumentsUsingFindWithQueryFilter() {
|
||||
Bson filter = eq("department", "Engineering");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
}
|
||||
|
||||
public static void retrieveAllDocumentsUsingFindWithQueryFilterAndProjection() {
|
||||
Bson filter = eq("department", "Engineering");
|
||||
Bson projection = fields(include("name", "age"));
|
||||
FindIterable<Document> documents = collection.find(filter)
|
||||
.projection(projection);
|
||||
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
}
|
||||
|
||||
public static void retrieveFirstDocument() {
|
||||
FindIterable<Document> documents = collection.find();
|
||||
Document document = documents.first();
|
||||
|
||||
System.out.println(document);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
setUp();
|
||||
|
||||
retrieveAllDocumentsUsingFind();
|
||||
|
||||
retrieveAllDocumentsUsingFindWithQueryFilter();
|
||||
|
||||
retrieveAllDocumentsUsingFindWithQueryFilterAndProjection();
|
||||
|
||||
retrieveFirstDocument();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -0,0 +1,94 @@
|
||||
package com.baeldung.mongo.find;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
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.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import static com.mongodb.client.model.Filters.eq;
|
||||
import static com.mongodb.client.model.Projections.fields;
|
||||
import static com.mongodb.client.model.Projections.include;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class FindOperationLiveTest {
|
||||
|
||||
private static MongoClient mongoClient;
|
||||
private static MongoDatabase database;
|
||||
private static MongoCollection<Document> collection;
|
||||
private static final String DATASET_JSON = "/employee.json";
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws IOException {
|
||||
if (mongoClient == null) {
|
||||
mongoClient = new MongoClient("localhost", 27017);
|
||||
|
||||
database = mongoClient.getDatabase("baeldung");
|
||||
collection = database.getCollection("employee");
|
||||
|
||||
collection.drop();
|
||||
|
||||
InputStream is = FindOperationLiveTest.class.getResourceAsStream(DATASET_JSON);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
reader.lines()
|
||||
.forEach(line -> collection.insertOne(Document.parse(line)));
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeCollection_whenFetchingUsingFindOperations_thenCheckingForDocuments() {
|
||||
FindIterable<Document> documents = collection.find();
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeCollection_whenFetchingUsingFindOperationsWithFilters_thenCheckingForDocuments() {
|
||||
Bson filter = eq("department", "Engineering");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeCollection_whenFetchingUsingFindOperationsWithFiltersAndProjection_thenCheckingForDocuments() {
|
||||
Bson filter = eq("department", "Engineering");
|
||||
Bson projection = fields(include("name", "age"));
|
||||
FindIterable<Document> documents = collection.find(filter)
|
||||
.projection(projection);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeCollection_whenFetchingFirstDocumentUsingFindOperations_thenCheckingForDocument() {
|
||||
Document employee = collection.find()
|
||||
.first();
|
||||
|
||||
assertNotNull(employee);
|
||||
assertFalse(employee.isEmpty());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
mongoClient.close();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,3 @@
|
||||
{"employeeId":"EMP1","name":"Sam","age":23,"type":"Full Time","department":"Engineering"}
|
||||
{"employeeId":"EMP2","name":"Tony","age":31,"type":"Full Time","department":"Admin"}
|
||||
{"employeeId":"EMP3","name":"Lisa","age":42,"type":"Part Time","department":"Engineering"}
|
@ -44,6 +44,7 @@
|
||||
<module>java-jpa-3</module>
|
||||
<module>java-mongodb</module> <!-- long running -->
|
||||
<module>java-mongodb-2</module> <!-- long running -->
|
||||
<module>java-mongodb-3</module> <!-- long running -->
|
||||
<module>jnosql</module> <!-- long running -->
|
||||
<module>jooq</module>
|
||||
<module>jpa-hibernate-cascade-type</module>
|
||||
|
Loading…
x
Reference in New Issue
Block a user