A name-value pair associates a named field with the value stored under it. MongoDB documents use these pairs to represent structured information, including strings, numbers, arrays, and nested documents. section 2.6.4 section 1.1
In MongoDB documents, name-value pairs support practical data modeling and access:
- Represent simple attributes such as a book's title, publisher, status, or publication year. section 1.1
- Store compound values such as arrays and nested documents under a single field. section 2.6.4 section 1.1
- Support querying, updating, and deleting documents by field values through the MongoDB Query API. section 1.1
- Keep related information in one document, which can reduce reads and simplify retrieval. section 1.1
Student record fields
A student document can associate _id with an ObjectId, student_id with 0, scores with an array of assessment objects, and class_id with 339. Each assessment object adds its own type and score pairs. section 2.6.4
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>
Inspect a complete MongoDB document and see simple fields alongside arrays of nested name-value pairs. section 2.6.4










