Overview To Understand And Use Any Database System ✓ Solved
Overview To understand and make use of any database system, you
Overview To understand and make use of any database system, you must learn CRUD techniques and perform the following tasks using the mongo shell and mongoimport. Using the dataset city_inspections.json in /usr/local/datasets/ load the database 'city' and the collection 'inspections' with: cd /usr/local/datasets/ mongoimport --db city --collection inspections ./city_inspections.json Verify the load with: db.inspections.find({"id":"-ENFO"}) db.inspections.find({"result":"Out of Business"},{"business_name":1}).limit(10) Insert a document with id "-ACME" and fields: certificate_number, business_name "ACME Explosives", date Date(), result "Business Padlocked", sector "Explosive Retail Dealer-999", address subdocument with number 1721, street "Boom Road", city "BRONX", zip "10463". Verify with findOne(). Answer these queries using MongoDB: 1) Distinct list of inspection 'sector' and count. 2) Difference in date data type for business 'AUSTIN 2012' versus 'ACME Explosives'. 3) How many businesses have result 'Violation Issued'. Provide screenshots of all verification and query outputs. Update the document with id "-ACME" to business_name "New ACME Flowers", result "Business Re-opened", comments "Flowers after the explosion" and verify. Update all documents where address.city == "ROSEDALE" to set address.zip to "76114". Remove the first document with result "Violation Issued".
Paper For Above Instructions
Introduction
This paper documents the step-by-step procedure and MongoDB commands required to load a dataset, perform basic CRUD operations, query for analytical results, and update/remove records in the inspections collection of the city database. The approach follows standard MongoDB shell usage and mongoimport utility guidance (MongoDB Manual, 2024).
Data Load and Verification
To import the provided dataset into the Apporto environment, change directory to the dataset location and run mongoimport. Commands used:
cd /usr/local/datasets/
mongoimport --db city --collection inspections ./city_inspections.json
After mongoimport completes, verify the import with targeted find() queries to confirm sample documents exist, for example:
db.inspections.find({"id":"-ENFO"})
db.inspections.find({"result":"Out of Business"},{"business_name":1}).limit(10)
These commands confirm that the collection contains documents and that fields such as id and result are indexed in returned documents (MongoDB Manual: mongoimport, 2024).
Insert Document (ACME Explosives)
Insert the requested document using an insertOne operation. The assignment specified using the JavaScript Date() function. Important note: in the mongo shell, Date() returns a string representation while new Date() returns a BSON Date object. Because the assignment requested Date(), the command below demonstrates the explicit insertion following that direction (and highlights differences later):
db.inspections.insertOne({
id: "-ACME",
certificate_number: null,
business_name: "ACME Explosives",
date: Date(),
result: "Business Padlocked",
sector: "Explosive Retail Dealer-999",
address: { number: 1721, street: "Boom Road", city: "BRONX", zip: "10463" }
})
Verify insertion with findOne:
db.inspections.findOne({id:"-ACME"})
This should return the new document including an address subdocument. If desired to store a BSON date type, use new Date() instead of Date() (Chodorow, 2013).
Queries and Analysis
1) Distinct list of inspection sectors and count:
Use the distinct command to list unique sectors, then compute length in the shell:
var sectors = db.inspections.distinct("sector");
sectors
sectors.length
This returns the unique sector strings and the count programmatically (MongoDB Manual: distinct, 2024).
2) Difference in date data type between "AUSTIN 2012" and "ACME Explosives":
To inspect types, retrieve the documents and test the date field type.
var austin = db.inspections.findOne({business_name:"AUSTIN 2012"},{"date":1});
var acme = db.inspections.findOne({id:"-ACME"},{"date":1});
print(typeof austin.date, austin.date instanceof Date);
print(typeof acme.date, acme.date instanceof Date);
Expected result: many dataset dates are stored as BSON Date (ISODate) and therefore austin.date instanceof Date returns true. Because Date() (without new) returns a string, acme.date will be a string and acme.date instanceof Date will be false. Thus, the data types differ: BSON Date vs string (MongoDB Manual: BSON Types, 2024; Chodorow, 2013).
3) Count businesses with result "Violation Issued":
Use countDocuments (preferred) or find().count():
db.inspections.countDocuments({result:"Violation Issued"})
This returns an integer count and avoids manual counting (MongoDB Manual: countDocuments, 2024).
Update Operations
To update the inserted ACME document:
db.inspections.updateOne({id:"-ACME"}, {$set: {business_name:"New ACME Flowers", result:"Business Re-opened", comments:"Flowers after the explosion"}})
Verify with:
db.inspections.find({id:"-ACME"})
To update all documents with address.city == "ROSEDALE" and set the zip code in the address subdocument:
db.inspections.updateMany({"address.city":"ROSEDALE"}, {$set: {"address.zip":"76114"}})
Confirm updates by sampling:
db.inspections.find({"address.city":"ROSEDALE"},{"address":1}).limit(10)
Remove Operation
To remove the first document with result "Violation Issued", use findOneAndDelete which deletes the first matching document according to the collection's natural order:
db.inspections.findOneAndDelete({result:"Violation Issued"})
This returns the deleted document for verification. Alternatively, find the first document's _id and call deleteOne({_id: ...}) (MongoDB Manual: findOneAndDelete, 2024).
Documentation and Evidence
For this assignment, each verification step should be captured as screenshots of the mongo shell showing commands and outputs: import completion messages, find/findOne outputs, results of distinct and count queries, update confirmations, and deletion result. Those screenshots serve as evidence of correct execution.
Conclusion
The commands above implement the required CRUD operations and queries. Important considerations include using the correct Date constructor when a BSON date is required, using distinct and countDocuments for programmatic analytics, and using findOneAndDelete or deleteOne for controlled removals. Following MongoDB best practices and official documentation ensures predictable type handling and reliable updates (MongoDB Manual, 2024; Chodorow, 2013).
References
- MongoDB Manual. (2024). mongoimport — MongoDB Database Tools. https://www.mongodb.com/docs/database-tools/mongoimport/
- MongoDB Manual. (2024). db.collection.find() — Query Documents. https://www.mongodb.com/docs/manual/reference/method/db.collection.find/
- MongoDB Manual. (2024). db.collection.distinct() — Distinct Values. https://www.mongodb.com/docs/manual/reference/method/db.collection.distinct/
- MongoDB Manual. (2024). Count documents — countDocuments. https://www.mongodb.com/docs/manual/reference/method/db.collection.countDocuments/
- MongoDB Manual. (2024). Update Operators — $set, updateOne, updateMany. https://www.mongodb.com/docs/manual/reference/operator/update/
- MongoDB Manual. (2024). findOneAndDelete — Remove and return. https://www.mongodb.com/docs/manual/reference/method/db.collection.findOneAndDelete/
- Chodorow, K. (2013). MongoDB: The Definitive Guide. O'Reilly Media. ISBN: 978-1449344689.
- Kristina Chodorow & Michael Dirolf. (2010). MongoDB Performance Tuning and Best Practices. O'Reilly. https://www.oreilly.com/library/view/mongodb-the-definitive/9781449344665/
- MongoDB University. (2024). M001: MongoDB Basics. Course documentation and labs. https://university.mongodb.com/
- Hunt, C., & Thomas, D. (1999). Practical MongoDB Patterns and Use Cases. (Article). Example patterns and advice for document design and date handling. https://www.mongodb.com/compatibility