liveBook · Manning

Name-value pair

As explained in MongoDB 8.0 in Action, Third Edition

A key-value pair is an association between a named key and the value stored under that key.

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

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines field-value associations and demonstrates simple, array, and nested values, without descending into BSON encoding internals.
Examples
Uses book documents and a student academic record containing identifiers, scalar fields, arrays, and nested assessment objects.
What it assumes
Relates the concept to maps, hashes, and dictionaries while introducing MongoDB's document model and BSON-supported values.
What you can do after
Apply name-value pairs when reading MongoDB documents and forming queries, updates, and deletions by field values.
How it connects
Connects name-value pairs with MongoDB documents, BSON, JSON, arrays, nested documents, and the MongoDB Query API.

Also explained in

Compare how 11 books explain name-value pair →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.