Using a Java Client with MongoDB: Getting Data from MongoDB

In this section we shall fetch data from MongoDB. We shall use the MongoDBClient application in this section. The MongoCollection<TDocument> interface provides the overloaded methods discussed in Table 1-7 to find documents.

  1. Create a MongoClient instance, a MongoDatabase instance, and a MongoCollection<TDocument> instance as discussed earlier.

MongoClient mongoClient = new MongoClient(Arrays.asList(new

ServerAddress(“localhost”, 27017)));

MongoDatabase db = mongoClient.getDatabase(“local”);

MongoCollection<Document> coll = db.getCollection(“catalog”);

  1. Create two Catalog instances and add the Catalog instances to the MongoCollection<TDocument> instance using the insertOne(TDocument document) method.

Document catalog = new Document(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”)

.append(“title”, “Engineering as a Service”)

.append(“author”, “David A. Kelly”);

coll.insertOne(catalog);

catalog = new Document(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”)

.append(“title”, “Quintessential and Collaborative”)

.append(“author”, “Tom Haunert”);

coll.insertOne(catalog);

  1. Subsequently, find the documents added using the find() method, which returns the result as a FindIterable <TDocument>.

FindIterable<Document> iterable = coll.find();

  1. Using an enhanced for loop iterate over the FindIterable<TDocument> to obtain the Document instances stored and obtain the key set associated with each Document instance. Create a iterator over the key set and using a while loop iterate over the key set to output each document key and Document object associated with each document key.

String documentKey = null;

for (Document document : iterable) {

Set<String> keySet = document.keySet();

Iterator<String> iter = keySet.iterator();

while (iter.hasNext()) {

documentKey = iter.next();

System.out.println(documentKey);

System.out.println(document.get(documentKey));

}

}

The MongoDBClient class is listed below.

package mongodb;

import java.util.Arrays;

import java.util.Iterator;

import java.util.Set;

import org.bson.Document;

import com.mongodb.MongoClient;

import com.mongodb.ServerAddress;

import com.mongodb.client.FindIterable;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;

public class MongoDBClient {

public static void main(String[] args) {

MongoClient mongoClient = new MongoClient(

Arrays.asList(new ServerAddress(“localhost”, 27017)));

MongoDatabase db = mongoClient.getDatabase(“local”);

MongoCollection<Document> coll = db.getCollection(“catalog);

Document catalog = new Document(“journal”, “Oracle Magazine”) .append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”)

.append(“title”, “Engineering as a Service”)

.append(“author”, “David A. Kelly”);

coll.insertOne(catalog);

catalog = new Document(“journal”, “Oracle Magazine”)

.append(“publisher”, “Oracle Publishing”)

.append(“edition”, “November December 2013”)

.append(“title”, “Quintessential and Collaborative”)

.append(“author”, “Tom Haunert”);

coll.insertOne(catalog);

FindIterable<Document> iterable = coll.find();

String documentKey = null;

for (Document document : iterable) {

Set<String> keySet = document.keySet();

Iterator<String> iter = keySet.iterator();

while (iter.hasNext()) {

documentKey = iter.next();

System.out.println(documentKey);

System.out.println(document.get(documentKey));

}

}

mongoClient.close();

}

}

  1. Before running the MongoDBClient application drop catalog collection from local database using the following commands from Mongo shell (which is discussed in more detail in the next chapter). The Mongo shell is started using the mongo command. Start the Mongo shell in a new command window and the MongoDB server instance should be running when the mongo shell commands are run.

>mongo

>use local

>db.catalog.drop()

6. To run the MongoDBClient application right-click on the MongoDBClient.java file in Package Explorer and select Run As ► Java Application as shown in Figure 1-15

.

The output from the application is displayed in the Eclipse Console as shown in Figure 1-16. The two documents added get fetched and the associated key/value pairs get output.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *