Updated article code (#12493)

This commit is contained in:
Avin Buricha 2022-07-15 00:08:20 +05:30 committed by GitHub
parent 3db189f71a
commit c252786fa9
3 changed files with 18 additions and 15 deletions

View File

@ -25,7 +25,7 @@ public class FindWithObjectId {
mongoClient = new MongoClient("localhost", 27017); mongoClient = new MongoClient("localhost", 27017);
databaseName = "baeldung"; databaseName = "baeldung";
collectionName = "employee"; collectionName = "vehicle";
database = mongoClient.getDatabase(databaseName); database = mongoClient.getDatabase(databaseName);
collection = database.getCollection(collectionName); collection = database.getCollection(collectionName);

View File

@ -26,7 +26,7 @@ public class FindWithObjectIdLiveTest {
private static MongoClient mongoClient; private static MongoClient mongoClient;
private static MongoDatabase database; private static MongoDatabase database;
private static MongoCollection<Document> collection; private static MongoCollection<Document> collection;
private static final String DATASET_JSON = "/employee.json"; private static final String DATASET_JSON = "/vehicle.json";
@BeforeClass @BeforeClass
public static void setUp() throws IOException { public static void setUp() throws IOException {
@ -34,7 +34,7 @@ public class FindWithObjectIdLiveTest {
mongoClient = new MongoClient("localhost", 27017); mongoClient = new MongoClient("localhost", 27017);
database = mongoClient.getDatabase("baeldung"); database = mongoClient.getDatabase("baeldung");
collection = database.getCollection("employee"); collection = database.getCollection("vehicle");
collection.drop(); collection.drop();
@ -47,10 +47,10 @@ public class FindWithObjectIdLiveTest {
} }
@Test @Test
public void givenEmployeeCollection_whenFetchingDocumentsUsingObjectId_thenCheckingForDocuments() { public void givenVehicleCollection_whenFetchingDocumentsUsingObjectId_thenCheckingForDocuments() {
Document employee = collection.find() Document vehicle = collection.find()
.first(); .first();
ObjectId objectId = (ObjectId) employee.get(OBJECT_ID_FIELD); ObjectId objectId = (ObjectId) vehicle.get(OBJECT_ID_FIELD);
FindIterable<Document> documents = collection.find(eq(OBJECT_ID_FIELD, objectId)); FindIterable<Document> documents = collection.find(eq(OBJECT_ID_FIELD, objectId));
MongoCursor<Document> cursor = documents.iterator(); MongoCursor<Document> cursor = documents.iterator();
@ -60,24 +60,24 @@ public class FindWithObjectIdLiveTest {
} }
@Test @Test
public void givenEmployeeCollection_whenFetchingFirstDocumentUsingObjectId_thenCheckingForDocument() { public void givenVehicleCollection_whenFetchingFirstDocumentUsingObjectId_thenCheckingForDocument() {
Document employee = collection.find() Document vehicle = collection.find()
.first(); .first();
ObjectId objectId = (ObjectId) employee.get(OBJECT_ID_FIELD); ObjectId objectId = (ObjectId) vehicle.get(OBJECT_ID_FIELD);
Document queriedEmployee = collection.find(eq(OBJECT_ID_FIELD, objectId)) Document queriedVehicle = collection.find(eq(OBJECT_ID_FIELD, objectId))
.first(); .first();
assertNotNull(queriedEmployee); assertNotNull(queriedVehicle);
assertEquals(employee.get(OBJECT_ID_FIELD), queriedEmployee.get(OBJECT_ID_FIELD)); assertEquals(vehicle.get(OBJECT_ID_FIELD), queriedVehicle.get(OBJECT_ID_FIELD));
} }
@Test @Test
public void givenEmployeeCollection_whenFetchingUsingRandomObjectId_thenCheckingForDocument() { public void givenVehicleCollection_whenFetchingUsingRandomObjectId_thenCheckingForDocument() {
Document employee = collection.find(eq(OBJECT_ID_FIELD, new ObjectId())) Document vehicle = collection.find(eq(OBJECT_ID_FIELD, new ObjectId()))
.first(); .first();
assertNull(employee); assertNull(vehicle);
} }
@AfterClass @AfterClass

View File

@ -0,0 +1,3 @@
{"companyName":"Skoda","modelName":"Octavia","launchYear":2016,"type":"Sports","registeredNo":"SKO 1134"}
{"companyName":"BMW","modelName":"X5","launchYear":2020,"type":"SUV","registeredNo":"BMW 3325"}
{"companyName":"Mercedes","modelName":"Maybach","launchYear":2021,"type":"Luxury","registeredNo":"MER 9754"}