Question

In: Computer Science

Develop a function to read a document in the MongoDB database “city” in the collection “inspections.”...

Develop a function to read a document in the MongoDB database “city” in the collection “inspections.” Be sure it can handle error conditions gracefully.
a. Input -> arguments to function should be the key/value lookup pair to use with the MongoDB driver find API call
b. return -> result in JSON format if successful else MongoDB returned error message

Solutions

Expert Solution

1) Here I am using Java to create a method which would get retire data from mongo db based on key/value argument

// getDataFromMongoDB is a method that would take two arguments of string type

public JSONObject getDataFromMongoDB(string key,string value){

// Initialize name of mongo database

String mongoDB="city";

//initialize name of mongo db collection

string mongoCollection="inspections";

// Connection string to mongo db that takes a end point to mongo db

MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");

//create object of mongoclient class

MongoClient mongoClient = new MongoClient(connectionString);

// Get object of mongo database

MongoDatabase database = mongoClient.getDatabase(mongoDB);

//Get the mongo collection object

MongoCollection<Document> collection = database.getCollection(mongoCollection);

// Find the first document in which key-value pair is matched

Document myDoc = collection.find(eq(key, value)).first();

//convert myDoc to json object and return

return myDoc.toJson();

}


Related Solutions

Very carefully read the following data requirements for a prospective document translation database: i. Document translation...
Very carefully read the following data requirements for a prospective document translation database: i. Document translation initially relies on a source document. The source document is a text provided in the originally recorded, valid language (see below, and assume only one language for the original document), and has an associated author and publication date. Source document authors are not related to translators in any manner. ii. Translation of a document also relies on a translator, who is capable of translating...
Compare the Data Modeling difference between relational database and NoSQL MongoDB.
Compare the Data Modeling difference between relational database and NoSQL MongoDB.
You are asked to develop a database for your city. Which of the following statements are appropriate?
You are asked to develop a database for your city. Which of the following statements are appropriate?You look for a database that offers transaction processing for municipal dataYour application should most likely be based on a general purpose database management system, and the meta data specific to your application will be stored in much the same way as the dataIf you can find a vendor for an application that is specific to your needs, such an application will likely have...
Read the narrative below for the City Healthcare Management System, and develop a CLASS DIAGRAM with...
Read the narrative below for the City Healthcare Management System, and develop a CLASS DIAGRAM with the following requirements: 1. List the attributes in each class (it is not given in the narrative), 2. For each class identify the key attribute, 3. Link the classes by drawing the associations between the classes, 4. For each association write the minimum and maximum multiplicity. Normally, one or many doctors, one or many nurse, and admin employees work in the clinic. The clinic...
Database Design A database is an electronic filing system: a collection of data organized for use...
Database Design A database is an electronic filing system: a collection of data organized for use and retrieval. Database design is the process of creating a model of the database. The model can include several attributes and entities, such as users, logic, physical storage locations, relationships, and other parameters. Write a post in which you explain at least three ways that database design and data architecture can work together to improve patient satisfaction, lower care costs, or improve patient outcomes....
Using MongoDB, what command would you enter to retrieve this document using only the zip code...
Using MongoDB, what command would you enter to retrieve this document using only the zip code 11242? db.inspections.find(???????) Database: city Collection: inspections {"_id":{"$oid":"56d61033a378eccde8a898ae"}, "id":"23536-2015-ENFO", "certificate_number":5373970, "business_name":"NZO CORP.", "date":"Apr 22 2015", "result":"Violation Issued", "sector":"Grocery-Retail - 808", "address":{"city":"BROOKLYN", "zip":11242, "street":"COURT ST", "number":26}}
The City of Imperial Falls contracts with Evergreen Waste Collection to provide solid waste collection to...
The City of Imperial Falls contracts with Evergreen Waste Collection to provide solid waste collection to households and businesses. Until recently, Evergreen had an exclusive franchise to provide this service in Imperial Falls, which meant that other waste collection firms could not operate legally in the city. The price per pound of waste collected was regulated at 25 percent above the average total cost of collection. Cost data for the most recent year of operations for Evergreen are as follows....
The City of Imperial Falls contracts with Evergreen Waste Collection to provide solid waste collection to...
The City of Imperial Falls contracts with Evergreen Waste Collection to provide solid waste collection to households and businesses. Until recently, Evergreen had an exclusive franchise to provide this service in Imperial Falls, which meant that other waste collection firms could not operate legally in the city. The price per pound of waste collected was regulated at 20 percent above the average total cost of collection. Cost data for the most recent year of operations for Evergreen are as follows....
The City of Imperial Falls contracts with Evergreen Waste Collection to provide solid waste collection to...
The City of Imperial Falls contracts with Evergreen Waste Collection to provide solid waste collection to households and businesses. Until recently, Evergreen had an exclusive franchise to provide this service in Imperial Falls, which meant that other waste collection firms could not operate legally in the city. The price per pound of waste collected was regulated at 20 percent above the average total cost of collection. Cost data for the most recent year of operations for Evergreen are as follows....
The City of Imperial Falls contracts with Evergreen Waste Collection to provide solid waste collection to...
The City of Imperial Falls contracts with Evergreen Waste Collection to provide solid waste collection to households and businesses. Until recently, Evergreen had an exclusive franchise to provide this service in Imperial Falls, which meant that other waste collection firms could not operate legally in the city. The price per pound of waste collected was regulated at 20 percent above the average total cost of collection. Cost data for the most recent year of operations for Evergreen are as follows:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT