Course: Data Science and Database Technologies
Source: Introduction to MongoDB, Daniele Apiletti (Politecnico di Torino / Data Base and Data Mining Group, DBMG), local path 02-MongoDB-intro-GUI.pdf, 36 PDF pages. Lecture number 22 is a local organizational number preserving 20 for 20-ElasticSearch_v2.pdf, while the source filename 02-MongoDB-intro-GUI.pdf reflects its numbering inside the NoSQL module.

Lecture summary

MongoDB is the leading open-source NoSQL document-oriented database, designed for high performance, high availability, native horizontal scalability, and flexible data modeling. Replacing relational tables, records, and columns with collections, documents, and fields, MongoDB stores records in BSON (Binary JSON)—supporting nested sub-documents, arrays, Date, NumberLong, and ObjectId. Every document reserves _id as an immutable, unique primary key (which may be any BSON type except an array), caps individual BSON documents at 16 megabytes (using GridFS for larger payloads), preserves field order, and supports schema-less flexibility alongside optional $jsonSchema document validation (introduced in MongoDB 3.2).

Shell administration manages databases and collections (show databases, use, show collections, createCollection, drop, dropDatabase), with collections created implicitly upon first reference. Core CRUD operations mirror SQL manipulation without rigid schemas: insertOne and insertMany for single, nested, array-bearing, or heterogeneous documents; find(criteria, projection).limit() for reads; updateMany(filter, { $set: ... }) for updates; and deleteMany(filter) (including deleteMany({}) to clear an entire collection).

For visual exploration and pipeline analytics, MongoDB Compass connects to local or remote instances (port 27017) to inspect list/table views, analyze schemas and geospatial coordinates ($geoWithin / $centerSphere), build queries with autocomplete, inspect Explain Plans for missing indexes, and configure validation rules. Multi-stage aggregation pipelines chain operators such as $group (mapping _id to SQL GROUP BY, $sum: 1 to COUNT(*), and $avg to AVG) and $match—either after $group to filter computed aggregates like SQL HAVING (as in the dbdmg.Parkings vendor pipeline) or before $group to filter raw document fields like SQL WHERE.

Learning route

  1. Revisit prerequisite context: MongoDB Polyglot Persistence Hadoop and Apache Spark.
  2. Read MongoDB BSON Documents Collections and CRUD Operations for relational-to-MongoDB terminology, BSON rules (_id, 16 MB / GridFS, ordering), database/collection commands, and CRUD syntax.
  3. Read MongoDB Compass and Aggregation Pipelines for Compass visual tooling (geospatial, Explain Plan, validation), aggregation pipelines ($group, $match), and WHERE vs. HAVING stage ordering.

Source map

PDF pagesMaterial
1–5Title (Daniele Apiletti, Politecnico di Torino / DBMG), MongoDB overview, Relational → MongoDB mapping, BSON document examples, and field/size rules (_id, GridFS, ordering)
6–11Databases and collections: schema-less flexibility, validation (3.2+), show databases, use, implicit creation, createCollection, show collections, drop, dropDatabase
12–23CRUD overview and shell examples: insertOne (flat, array, nested), insertMany, find, updateMany, and deleteMany (WHERE vs. deleteMany({}))
24–32MongoDB Compass GUI: connection, list/table views, schema & geospatial $geoWithin queries, query bar autocomplete, Explain Plan, and $jsonSchema validation
33–36Aggregation pipelines in Compass: $group (_id = GROUP BY, $sum: 1, $avg) and multi-stage $group → $match pipeline on dbdmg.Parkings

Worked examples from the source

1. BSON documents & CRUD on people / users (Slides 4, 12–23)

  • Rich BSON document (Slide 4): { _id: ObjectId(...), name: { first: "Alan", last: "Turing" }, birth: new Date('Jun 23, 1912'), contribs: ["Turing machine", "Turing test", "Turingery"], views: NumberLong(1250000) }.
  • CRUD (Slides 12–23): db.users.find({ age: { $gt: 18 } }, { name: 1, address: 1 }).limit(5), db.users.updateMany({ age: { $lt: 18 } }, { $set: { status: "reject" } }), and db.people.deleteMany({ status: "D" }) vs. db.people.deleteMany({}).

2. Two-stage aggregation pipeline on dbdmg.Parkings (Slides 34–36)

  1. Stage 1 ($group): { _id: "$vendor", total: { $sum: 1 }, avg_fuel: { $avg: "$fuel" } } produces { _id: "car2go", total: 48423, avg_fuel: 64.8828... } and { _id: "enjoy", total: 30134, avg_fuel: 61.0338... }.
  2. Stage 2 ($match): { avg_fuel: { $gt: 63 }, total: { $gt: 35000 } } filters on the grouped fields (like SQL HAVING), leaving only car2go.

(See MongoDB Compass and Aggregation Pipelines for the WHERE vs. HAVING stage-ordering explanation and an added $match → $group practice example outside the PDF.)

Revision prompts

  • List the key BSON rules for _id, document size limits (>16 MB), dots/$, duplicate keys, and field ordering.
  • Translate INSERT, SELECT ... WHERE ... LIMIT, UPDATE ... SET ... WHERE, and DELETE FROM (with and without WHERE) into MongoDB shell CRUD calls.
  • Explain what MongoDB Compass’s Schema, Explain Plan, and Validation tabs show and how geospatial $geoWithin filtering works.
  • Contrast when $match must go before $group (WHERE) vs. after $group (HAVING), and write a 2-stage pipeline for SELECT city, COUNT(*), AVG(fuel) FROM Parkings WHERE fuel > 20 GROUP BY city.

Source and scope

Daniele Apiletti (Politecnico di Torino / Data Base and Data Mining Group), Introduction to MongoDB, local path 02-MongoDB-intro-GUI.pdf, 36 PDF pages. This note paraphrases and organizes the source material; added study explanations and the $match → $group SQL translation example in the linked concept note are labeled explicitly.