BAEL-5357: Retrieve a Value from MongoDB by Its Key Name (#12005)
This commit is contained in:
parent
b1c60e9093
commit
007271ec3b
|
@ -0,0 +1,118 @@
|
||||||
|
package com.baeldung.mongo;
|
||||||
|
|
||||||
|
import static com.mongodb.client.model.Aggregates.project;
|
||||||
|
import static com.mongodb.client.model.Projections.fields;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.bson.Document;
|
||||||
|
import org.bson.conversions.Bson;
|
||||||
|
|
||||||
|
import com.mongodb.BasicDBObject;
|
||||||
|
import com.mongodb.DB;
|
||||||
|
import com.mongodb.DBCollection;
|
||||||
|
import com.mongodb.DBCursor;
|
||||||
|
import com.mongodb.MongoClient;
|
||||||
|
import com.mongodb.client.MongoDatabase;
|
||||||
|
import com.mongodb.client.model.Projections;
|
||||||
|
|
||||||
|
public class RetrieveValue {
|
||||||
|
|
||||||
|
private static MongoClient mongoClient;
|
||||||
|
private static MongoDatabase database;
|
||||||
|
private static String testCollectionName;
|
||||||
|
private static String databaseName;
|
||||||
|
|
||||||
|
public static void setUp() {
|
||||||
|
if (mongoClient == null) {
|
||||||
|
mongoClient = new MongoClient("localhost", 27017);
|
||||||
|
}
|
||||||
|
|
||||||
|
databaseName = "baeldung";
|
||||||
|
testCollectionName = "travel";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void retrieveValueUsingFind() {
|
||||||
|
|
||||||
|
DB database = mongoClient.getDB(databaseName);
|
||||||
|
DBCollection collection = database.getCollection(testCollectionName);
|
||||||
|
|
||||||
|
BasicDBObject queryFilter = new BasicDBObject();
|
||||||
|
|
||||||
|
BasicDBObject projection = new BasicDBObject();
|
||||||
|
projection.put("passengerId", 1);
|
||||||
|
projection.put("_id", 0);
|
||||||
|
|
||||||
|
DBCursor dbCursor = collection.find(queryFilter, projection);
|
||||||
|
while (dbCursor.hasNext()) {
|
||||||
|
System.out.println(dbCursor.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void retrieveValueUsingAggregation() {
|
||||||
|
|
||||||
|
ArrayList<Document> response = new ArrayList<>();
|
||||||
|
|
||||||
|
ArrayList<Bson> pipeline = new ArrayList<>(Arrays.asList(
|
||||||
|
|
||||||
|
project(fields(Projections.exclude("_id"), Projections.include("passengerId")))));
|
||||||
|
|
||||||
|
database = mongoClient.getDatabase(databaseName);
|
||||||
|
|
||||||
|
database.getCollection(testCollectionName)
|
||||||
|
.aggregate(pipeline)
|
||||||
|
.allowDiskUse(true)
|
||||||
|
.into(response);
|
||||||
|
|
||||||
|
System.out.println("response:- " + response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void retrieveValueAggregationUsingDocument() {
|
||||||
|
|
||||||
|
ArrayList<Document> response = new ArrayList<>();
|
||||||
|
|
||||||
|
ArrayList<Document> pipeline = new ArrayList<>(Arrays.asList(new Document("$project", new Document("passengerId", 1L))));
|
||||||
|
|
||||||
|
database = mongoClient.getDatabase(databaseName);
|
||||||
|
|
||||||
|
database.getCollection(testCollectionName)
|
||||||
|
.aggregate(pipeline)
|
||||||
|
.allowDiskUse(true)
|
||||||
|
.into(response);
|
||||||
|
|
||||||
|
System.out.println("response:- " + response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
|
||||||
|
//
|
||||||
|
// Connect to cluster (default is localhost:27017)
|
||||||
|
//
|
||||||
|
setUp();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Fetch the data using find query with projected fields
|
||||||
|
//
|
||||||
|
|
||||||
|
retrieveValueUsingFind();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Fetch the data using aggregate pipeline query with projected fields
|
||||||
|
//
|
||||||
|
|
||||||
|
retrieveValueUsingAggregation();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Fetch the data using aggregate pipeline document query with projected fields
|
||||||
|
//
|
||||||
|
|
||||||
|
retrieveValueAggregationUsingDocument();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,114 @@
|
||||||
|
package com.baeldung.mongo;
|
||||||
|
|
||||||
|
import static com.mongodb.client.model.Aggregates.project;
|
||||||
|
import static com.mongodb.client.model.Projections.fields;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.bson.Document;
|
||||||
|
import org.bson.conversions.Bson;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.mongodb.BasicDBObject;
|
||||||
|
import com.mongodb.DB;
|
||||||
|
import com.mongodb.DBCollection;
|
||||||
|
import com.mongodb.DBCursor;
|
||||||
|
import com.mongodb.MongoClient;
|
||||||
|
import com.mongodb.client.MongoCollection;
|
||||||
|
import com.mongodb.client.MongoDatabase;
|
||||||
|
import com.mongodb.client.model.Filters;
|
||||||
|
import com.mongodb.client.model.Projections;
|
||||||
|
|
||||||
|
public class RetrieveValueLiveTest {
|
||||||
|
|
||||||
|
private static MongoClient mongoClient;
|
||||||
|
private static MongoDatabase database;
|
||||||
|
private static MongoCollection<Document> collection;
|
||||||
|
private static final String DATASET_JSON = "/travel.json";
|
||||||
|
private static DB db;
|
||||||
|
private static DBCollection dbCollection;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setup() throws IOException {
|
||||||
|
if (mongoClient == null) {
|
||||||
|
mongoClient = new MongoClient("localhost", 27017);
|
||||||
|
database = mongoClient.getDatabase("baeldung");
|
||||||
|
db = mongoClient.getDB("baeldung");
|
||||||
|
dbCollection = db.getCollection("travel");
|
||||||
|
collection = database.getCollection("travel");
|
||||||
|
collection.drop();
|
||||||
|
|
||||||
|
InputStream is = BulkOperationLiveTest.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 givenTravelCollection_whenfetchUsingFindOperations_thenCheckingForDocument() {
|
||||||
|
|
||||||
|
BasicDBObject queryFilter = new BasicDBObject();
|
||||||
|
|
||||||
|
BasicDBObject projection = new BasicDBObject();
|
||||||
|
projection.put("passengerId", 1);
|
||||||
|
projection.put("_id", 0);
|
||||||
|
|
||||||
|
DBCursor dbCursor = dbCollection.find(queryFilter, projection);
|
||||||
|
while (dbCursor.hasNext()) {
|
||||||
|
System.out.println(dbCursor.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
Document travelDetail = collection.find(Filters.eq("passengerId", 145))
|
||||||
|
.first();
|
||||||
|
assertNotNull(travelDetail);
|
||||||
|
assertFalse(travelDetail.isEmpty());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTravelCollection_whenfetchUsingAggregationOperations_thenCheckingForDocument() {
|
||||||
|
|
||||||
|
ArrayList<Document> response = new ArrayList<>();
|
||||||
|
ArrayList<Bson> pipeline = new ArrayList<>(Arrays.asList(project(fields(Projections.exclude("_id"), Projections.include("passengerId")))));
|
||||||
|
collection.aggregate(pipeline)
|
||||||
|
.allowDiskUse(true)
|
||||||
|
.into(response);
|
||||||
|
|
||||||
|
Document travelDetail = collection.find(Filters.eq("passengerId", 145))
|
||||||
|
.first();
|
||||||
|
assertNotNull(travelDetail);
|
||||||
|
assertFalse(travelDetail.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTravelCollection_whenfetchUsingAggregationUsingDocumentOperations_thenCheckingForDocument() {
|
||||||
|
|
||||||
|
ArrayList<Document> response = new ArrayList<>();
|
||||||
|
ArrayList<Document> pipeline = new ArrayList<>(Arrays.asList(new Document("$project", new Document("passengerId", 1L))));
|
||||||
|
collection.aggregate(pipeline)
|
||||||
|
.allowDiskUse(true)
|
||||||
|
.into(response);
|
||||||
|
|
||||||
|
Document travelDetail = collection.find(Filters.eq("passengerId", 145))
|
||||||
|
.first();
|
||||||
|
assertNotNull(travelDetail);
|
||||||
|
assertFalse(travelDetail.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void cleanUp() {
|
||||||
|
mongoClient.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
{ "passengerId":145, "passengerName":"Nathan Green", "passengerAge":25, "sourceStation":"London","destinationStation":"Birmingham","seatType":"Slepper","emailAddress":"nathongreen12@gmail.com"}
|
||||||
|
{ "passengerId":148,"passengerName":"Kevin Joseph","passengerAge":28,"sourceStation":"Manchester","destinationStation":"London","seatType":"Slepper","emailAddress":"kevin13@gmail.com"}
|
||||||
|
{"passengerId":154,"passengerName":"Sheldon burns","passengerAge":26,"sourceStation":"Cambridge","destinationStation":"Leeds","seatType":"Slepper","emailAddress":"sheldonnn160@gmail.com"}
|
||||||
|
{"passengerId":168,"passengerName":"Jack Ferguson","passengerAge":24,"sourceStation":"Cardiff","destinationStation":"Coventry","seatType":"Slepper","emailAddress":"jackfergusion9890@gmail.com"}
|
Loading…
Reference in New Issue