MongoDB is a general-purpose document database for persisting application data. It stores records as BSON documents in collections. BSON extends JSON with data types such as dates and binary data; a document can contain arrays and nested documents. This makes it practical to keep related data together rather than transform every object into rows and tables. section 1.6 section 2.6.4
Documents in one collection can have different fields and data types, but a flexible schema is not a substitute for data modeling. Before production deployment, identify the application workload, map relationships, choose design patterns, and create indexes for important queries. Consider how the data and its access patterns will change. section 5.0 section 5.1
Inspect and change documents
In mongosh, select a database with `use sample_training`, then run `db.grades.findOne()` to inspect a record. Its `scores` array contains assessment documents, while `_id` identifies the record. For a books collection, `db.books.find({ "title": "MongoDB 8.0 in Action" })` retrieves matching documents; `db.books.updateOne({ "title": "MongoDB 8.0 in Action" }, { $set: { "publisher": "Manning Publications Co" } })` changes one matching document. section 2.6.4 section 1.1
The MongoDB Query API handles CRUD operations and multistage aggregation pipelines. Queries can filter, project, and sort results; aggregation can reshape data and combine collections. Indexes support efficient retrieval, while `explain()` exposes query-plan details when diagnosing a slow query. Without a suitable index, MongoDB may scan the full collection. section 1.5 section 7.8
For larger workloads, MongoDB supports scaling out through sharding, which distributes data and balances load across servers. Deployment choices include the free Community Edition, Enterprise Advanced with additional security and management capabilities, and MongoDB Atlas, a managed cloud service that simplifies setup and scaling and adds search capabilities. section 1.6 section 1.3
Listing 2.7 Displaying a single MongoDB document using findOne()
[primary] sample_training> use sample_training
already on db sample_training
[primary] sample_training> db.grades.findOne()
{
_id: ObjectId('56d5f7eb604eb380b0d8d8ce'),
student_id: 0,
scores: [
{ type: 'exam', score: 78.40446309504266 },
{ type: 'quiz', score: 73.36224783231339 },
{ type: 'homework', score: 46.980982486720535 },
{ type: 'homework', score: 76.67556138656222 }
],
class_id: 339
}
[primary] sample_training>
The `findOne()` result shows a complete BSON document, including its `_id` and an array of nested score records. section 2.6.4






