BAEL-5361 | Article code (#12455)

This commit is contained in:
Avin Buricha 2022-07-06 22:43:32 +05:30 committed by GitHub
parent c876ceb2e0
commit 9b4afd4ea3
2 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,80 @@
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.types.ObjectId;
import static com.mongodb.client.model.Filters.eq;
public class FindWithObjectId {
private static final String OBJECT_ID_FIELD = "_id";
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 retrieveAllDocumentsUsingFindWithObjectId() {
Document document = collection.find()
.first();
System.out.println(document);
FindIterable<Document> documents = collection.find(eq(OBJECT_ID_FIELD, document.get(OBJECT_ID_FIELD)));
MongoCursor<Document> cursor = documents.iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
public static void retrieveFirstDocumentWithObjectId() {
Document document = collection.find()
.first();
System.out.println(document);
FindIterable<Document> documents = collection.find(eq(OBJECT_ID_FIELD, document.get(OBJECT_ID_FIELD)));
Document queriedDocument = documents.first();
System.out.println(queriedDocument);
}
public static void retrieveDocumentWithRandomObjectId() {
FindIterable<Document> documents = collection.find(eq(OBJECT_ID_FIELD, new ObjectId()));
Document queriedDocument = documents.first();
if (queriedDocument != null) {
System.out.println(queriedDocument);
} else {
System.out.println("No documents found");
}
}
public static void main(String args[]) {
setUp();
retrieveAllDocumentsUsingFindWithObjectId();
retrieveFirstDocumentWithObjectId();
retrieveDocumentWithRandomObjectId();
}
}

View File

@ -0,0 +1,87 @@
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.types.ObjectId;
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 org.junit.Assert.*;
public class FindWithObjectIdLiveTest {
private static final String OBJECT_ID_FIELD = "_id";
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_whenFetchingDocumentsUsingObjectId_thenCheckingForDocuments() {
Document employee = collection.find()
.first();
ObjectId objectId = (ObjectId) employee.get(OBJECT_ID_FIELD);
FindIterable<Document> documents = collection.find(eq(OBJECT_ID_FIELD, objectId));
MongoCursor<Document> cursor = documents.iterator();
assertNotNull(cursor);
assertTrue(cursor.hasNext());
}
@Test
public void givenEmployeeCollection_whenFetchingFirstDocumentUsingObjectId_thenCheckingForDocument() {
Document employee = collection.find()
.first();
ObjectId objectId = (ObjectId) employee.get(OBJECT_ID_FIELD);
Document queriedEmployee = collection.find(eq(OBJECT_ID_FIELD, objectId))
.first();
assertNotNull(queriedEmployee);
assertEquals(employee.get(OBJECT_ID_FIELD), queriedEmployee.get(OBJECT_ID_FIELD));
}
@Test
public void givenEmployeeCollection_whenFetchingUsingRandomObjectId_thenCheckingForDocument() {
Document employee = collection.find(eq(OBJECT_ID_FIELD, new ObjectId()))
.first();
assertNull(employee);
}
@AfterClass
public static void cleanUp() {
mongoClient.close();
}
}