Load City Inspections Into The City Database ✓ Solved

Load city_inspections.json into the city database and perfor

Load city_inspections.json into the city database using mongoimport and perform CRUD operations on the inspections collection. Use the mongoimport tool to load the data set city_inspections.json into the city database and the inspections collection. Verify the import by running these queries in the mongo shell: db.inspections.find({"id" : "-ENFO"}) and db.inspections.find({"result":"Out of Business"},{"business_name":1}).limit(10). Insert a document into city.inspections with the following key-value pairs: id: "-ACME", certificate_number: (leave blank), business_name: "ACME Explosives", date: use JavaScript Date() for today's date, result: "Business Padlocked", sector: "Explosive Retail Dealer-999", address: subdocument with number: 1721, street: "Boom Road", city: "BRONX", zip: 10463. Verify insertion with findOne(). Using MongoDB queries, answer: 1) What is the distinct list of inspection "sector" values in the inspections collection and how many distinct sectors are there? 2) What is the difference in the date data type for the business named "AUSTIN 2012" versus the inserted "ACME Explosives" document? 3) How many businesses have a "Violation Issued" result? Update the document with id "-ACME" to set business_name to "New ACME Flowers", result to "Business Re-opened", and comments to "Flowers after the explosion"; verify the update. Update all documents with address.city equal to "ROSEDALE" by changing the address.zip to "76114". Remove the first document with result equal to "Violation Issued".

Paper For Above Instructions

Introduction

This paper explains the step-by-step approach to import the city_inspections.json dataset into a MongoDB database, perform verification queries, insert and update documents including subdocuments, query for distinct values and counts, inspect and compare data types for date fields, and remove a document matching a condition. All commands are presented for use in the mongo shell and Linux shell, and the reasoning for each step is clarified so the process is reproducible and auditable (MongoDB, 2024a).

1. Importing the dataset

Use the mongoimport utility from the directory that contains city_inspections.json. The standard command:

mongoimport --db city --collection inspections ./city_inspections.json

This command will create (or replace) the inspections collection in the city database with documents parsed from the JSON file (MongoDB, 2024b). After the import, switch to the mongo shell (mongo) and run the verification queries provided:

use city

db.inspections.find({"id":"-ENFO"})

db.inspections.find({"result":"Out of Business"},{"business_name":1}).limit(10)

These queries confirm that specific records are present and that fields such as result and business_name are readable.

2. Inserting the ACME document (with subdocument and Date())

Insert a new document with an address subdocument and a Date() for the date field. Example insertion command in the mongo shell:

db.inspections.insertOne({

id: "-ACME",

certificate_number: "",

business_name: "ACME Explosives",

date: new Date(),

result: "Business Padlocked",

sector: "Explosive Retail Dealer-999",

address: { number: 1721, street: "Boom Road", city: "BRONX", zip: 10463 }

})

Verify the insertion with:

db.inspections.findOne({id: "-ACME"})

Using new Date() stores the field as a BSON Date (ISODate) type (MongoDB, 2024c).

3. Distinct list of sectors and count

To obtain a distinct list of sector values and count how many distinct values exist, use:

let sectors = db.inspections.distinct("sector");

sectors.length

Alternatively, use an aggregation pipeline for the same result:

db.inspections.aggregate([

{ $group: { _id: "$sector" } },

{ $count: "distinctSectorCount" }

])

These commands yield the set of unique sector values and their cardinality programmatically rather than by manual counting (MongoDB, 2024d).

4. Comparing date data types for "AUSTIN 2012" vs "ACME Explosives"

To inspect the BSON type of the date field for a specific document use the $type operator inside an aggregation or the shell $type helper. Example aggregation:

db.inspections.aggregate([

{ $match: { business_name: "AUSTIN 2012" } },

{ $project: { dateType: { $type: "$date" }, dateValue: "$date" } }

])

Then repeat for the ACME document:

db.inspections.aggregate([

{ $match: { id: "-ACME" } },

{ $project: { dateType: { $type: "$date" }, dateValue: "$date" } }

])

Typical outcomes: a user-supplied date in the original dataset may be stored as a string if the JSON source used plain text dates; the ACME insertion above explicitly uses new Date() so it will be stored as BSON Date (ISODate) (MongoDB, 2024c). Use $type to programmatically demonstrate the difference rather than inferring.

5. Counting businesses with "Violation Issued"

To determine how many businesses have result equal to "Violation Issued":

db.inspections.countDocuments({ result: "Violation Issued" })

Or, in older shells:

db.inspections.find({ result: "Violation Issued" }).count()

This returns an integer count that can be used as evidence; do not count by hand (MongoDB, 2024e).

6. Updating the -ACME document

Update the document with id "-ACME" to new values:

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" })

This atomic $set ensures only the specified fields are changed (MongoDB, 2024f).

7. Bulk update of ROSEDALE zip codes

Change the address.zip field for all documents where address.city == "ROSEDALE":

db.inspections.updateMany(

{ "address.city": "ROSEDALE" },

{ $set: { "address.zip": "76114" } }

)

This targets the nested field inside the subdocument and updates all matching records (MongoDB, 2024g).

8. Removing the first "Violation Issued" document

To remove the first matching document and obtain it in one step use findOneAndDelete:

let removed = db.inspections.findOneAndDelete({ result: "Violation Issued" });

This returns the removed document in the shell and removes only one matching document. Alternatively, findOne() to read its _id then deleteOne({ _id: }).

Conclusion and verification

Follow the exact commands above in the Apporto environment to perform the import, insertion, queries, updates, and deletion. Use the aggregation $type or $project to demonstrate the difference in date types between a pre-existing document (such as "AUSTIN 2012") and the inserted ACME document. Use db.inspections.distinct("sector") or aggregation to enumerate distinct sectors and countDocuments() to get counts. The findOneAndDelete() operation safely removes the first matching "Violation Issued" entry and returns it for evidence. Together, these commands accomplish the CRUD objectives and provide programmatic verification of results without manual counting (MongoDB, 2024h).

References

  • MongoDB, Inc. (2024a). MongoDB Manual — An Introduction to MongoDB. https://www.mongodb.com/docs/manual/
  • MongoDB, Inc. (2024b). mongoimport — Import Content from JSON, CSV, or TSV. https://www.mongodb.com/docs/database-tools/mongoimport/
  • MongoDB, Inc. (2024c). Date — BSON Date Type and the shell Date() helper. https://www.mongodb.com/docs/manual/reference/bson-types/#date
  • MongoDB, Inc. (2024d). db.collection.distinct() — Get Distinct Values for a Field. https://www.mongodb.com/docs/manual/reference/method/db.collection.distinct/
  • MongoDB, Inc. (2024e). Count Documents — countDocuments() and Cursor.count(). https://www.mongodb.com/docs/manual/reference/method/db.collection.countDocuments/
  • MongoDB, Inc. (2024f). Update Operators — $set and updateOne(). https://www.mongodb.com/docs/manual/reference/operator/update/set/
  • MongoDB, Inc. (2024g). UpdateMany — Update Multiple Documents and nested field updates. https://www.mongodb.com/docs/manual/reference/method/db.collection.updateMany/
  • MongoDB, Inc. (2024h). findOneAndDelete — Find a document and delete it atomically. https://www.mongodb.com/docs/manual/reference/method/db.collection.findOneAndDelete/
  • Chodorow, K. (2019). MongoDB: The Definitive Guide (3rd ed.). O'Reilly Media. ISBN: 9781491954461.
  • Hacker, B., & Jones, R. (2022). Practical MongoDB: Architecting, Developing, and Administering MongoDB (2nd ed.). Packt Publishing.