How to Iterate through MongoDB Query Results through a Function using Java
Introduction
This tutorial will demonstrate how to iterate through MongoDB query results using Java. The tutorial will provide a sample MongoDB dataset and walk users through specific examples of how to set up the MongoDB connection and executes the java functions needed to perform the iteration of the MongoDB query results. The MongoDB and the compatible MongoDB Java Driver must be properly installed and running before beginning. The examples illustrated in this tutorial will use MongoDB version 4.0 and MongoDB Java Driver version 3.8.2.
Prerequisites
- The MongoDB and the compatible MongoDB Java Driver must be properly installed before beginning.
- The MongoDB service must be running.
NOTE: All of the examples in this tutorial use MongoDB version 4.0 and MongoDB Java Driver version 3.8.2
Working with a Sample MongoDB Data Set
- The sample MongoDB dataset used in this tutorial is as follows:
ID | Name | Country |
---|---|---|
5ceb7d233fd48a1ebd04318e | Chang Kai-shek | China |
5ceb7d233fd48a1ebd04318f | Sun Yat-sen | China |
5ceb7d233fd48a1ebd043190 | Muammar al-Gaddafi | Libya |
5ceb7d233fd48a1ebd043191 | Oda Nobunaga | Japan |
5ceb7d233fd48a1ebd043192 | Cao Cao | China |
5ceb7d233fd48a1ebd043193 | Sun Tzu | China |
- The result of a Mongo Shell query,
db.warlordCollection.find().pretty()
follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | { "_id" : ObjectId("5ceb7d233fd48a1ebd04318e"), "name" : "Chang Kai-shek", "country" : "china" } { "_id" : ObjectId("5ceb7d233fd48a1ebd04318f"), "name" : "Sun Yat-sen", "country" : "china" } { "_id" : ObjectId("5ceb7d233fd48a1ebd043190"), "name" : "Muammar al-Gaddafi", "country" : "Libya" } { "_id" : ObjectId("5ceb7d233fd48a1ebd043191"), "name" : "Oda Nobunaga", "country" : "Japan" } { "_id" : ObjectId("5ceb7d233fd48a1ebd043192"), "name" : "Cao Cao", "country" : "China" } { "_id" : ObjectId("5ceb7d233fd48a1ebd043193"), "name" : "Sun Tzu", "country" : "China" } |
How to Iterate Through a MongoDB Query Results Through a Function Using Java
- The below code shows how to perform an iteration to MongoDB query results:
How to create the MongoDB connection
1 2 3 | MongoClient mongo = MongoClients.create("mongodb://127.0.0.1:27017"); MongoDatabase db = mongo.getDatabase("warlordDB"); MongoCollection<document> collection = db.getCollection("warlordCollection"); |
- The above code establishes a connection to the MongoDB deployment and accesses both the database (warlordDB) and the specified collection (warlordCollection).
How to Create the code for the Java function
- The below code executes the java function to perform the iteration of the MongoDB query results.
NOTE: This function requires a collection to be passed in as its parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public void findWarlord(MongoCollection<document> coll) { FindIterable<document> iterable = coll.find(); // (1) MongoCursor<document> cursor = iterable.iterator(); // (2) try { while(cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } } |
- FindIterable — This will generate a more targeted method and allows access to other methods, such as:
A.
filter()
. B.limit()
. C.skip()
. - MongoCursor — The cursor that allows the iteration through the results of a database query.
How to Call the Java Function to Perform the Iteration
- The following code can be used to execute the Java function:
1 2 3 4 5 | MongoClient mongo = MongoClients.create("mongodb://127.0.0.1:27017"); MongoDatabase db = mongo.getDatabase("warlordDB"); MongoCollection<document> coll = db.getCollection("warlordCollection"); findWarlord(coll); |
NOTE: This code simply calls the function name findWarlord(coll);
and uses the collection variable coll
as its parameter.
- The results should resemble something that looks like the following:
1 2 3 4 5 6 | Document{{_id=5ceb7d233fd48a1ebd04318e, name=Chang Kai-shek, country=china}} Document{{_id=5ceb7d233fd48a1ebd04318f, name=Sun Yat-sen, country=china}} Document{{_id=5ceb7d233fd48a1ebd043190, name=Muammar al-Gaddafi, country=Libya}} Document{{_id=5ceb7d233fd48a1ebd043191, name=Oda Nobunaga, country=Japan}} Document{{_id=5ceb7d233fd48a1ebd043192, name=Cao Cao, country=China}} Document{{_id=5ceb7d233fd48a1ebd043193, name=Sun Tzu, country=China}} |
Conclusion
This tutorial demonstrated the basic ways on how to iterate through MongoDB query results through a function using Java. The specific examples demonstrated how to create the MongoDB connection, how to create the code for the java function and how to call the Java function to perform the iteration. Users must bear in mind the FindIterable command will allow access to other methods, such as filter, limit and skip, to aid in better qualifying the query results, and the MongoCursor cursor allows the iteration through the results of a database query. Users should also remember that all of the examples in this tutorial were executed using MongoDB version 4.0 and MongoDB Java Driver version 3.8.2. Using other versions of either the MongoDB or the MongoDB Java Driver may produce different results.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started