0
0
MongoDBquery~10 mins

One-to-many referencing pattern in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - One-to-many referencing pattern
Parent Document Created
Child Documents Created
Child Documents store Parent _id
Query Parent
Query Child Documents by Parent _id
Combine Parent with Children
Result
Create a parent document and multiple child documents that reference the parent's ID. Query parent and then find all children linked by that ID.
Execution Sample
MongoDB
db.authors.insertOne({_id: 1, name: 'Alice'})
db.books.insertMany([
  {title: 'Book A', author_id: 1},
  {title: 'Book B', author_id: 1}
])
db.authors.findOne({_id: 1})
db.books.find({author_id: 1})
Insert one author and two books referencing that author, then query the author and their books.
Execution Table
StepActionInput/QueryResult/Output
1Insert parent document{_id: 1, name: 'Alice'}Author document created with _id 1
2Insert child documents[{title: 'Book A', author_id: 1}, {title: 'Book B', author_id: 1}]Two book documents created referencing author_id 1
3Query parent document{_id: 1}{_id: 1, name: 'Alice'}
4Query child documents by parent _id{author_id: 1}[{title: 'Book A', author_id: 1}, {title: 'Book B', author_id: 1}]
5Combine parent and childrenParent + Children{author: {_id:1, name:'Alice'}, books: [{title:'Book A', author_id:1}, {title:'Book B', author_id:1}]}
6EndNo more stepsExecution complete
💡 All documents inserted and queried successfully, referencing pattern established.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
authors collectionempty[{_id:1, name:'Alice'}][{_id:1, name:'Alice'}][{_id:1, name:'Alice'}][{_id:1, name:'Alice'}][{_id:1, name:'Alice'}]
books collectionemptyempty[{title:'Book A', author_id:1}, {title:'Book B', author_id:1}][{title:'Book A', author_id:1}, {title:'Book B', author_id:1}][{title:'Book A', author_id:1}, {title:'Book B', author_id:1}][{title:'Book A', author_id:1}, {title:'Book B', author_id:1}]
queried parentnonenonenone{_id:1, name:'Alice'}{_id:1, name:'Alice'}{_id:1, name:'Alice'}
queried childrennonenonenonenone[{title:'Book A', author_id:1}, {title:'Book B', author_id:1}][{title:'Book A', author_id:1}, {title:'Book B', author_id:1}]
Key Moments - 3 Insights
Why do child documents store the parent's _id instead of embedding the whole parent document?
Storing only the parent's _id keeps child documents smaller and avoids duplication. This referencing allows easy updates to the parent without changing all children, as shown in execution_table steps 2 and 4.
How do we find all child documents related to a specific parent?
We query the child collection filtering by the parent's _id stored in the child documents (execution_table step 4). This returns all children linked to that parent.
What happens if the parent document is deleted but child documents still reference its _id?
Child documents will reference a non-existent parent, causing broken links. This is why application logic or database constraints should handle such cases, though MongoDB does not enforce it automatically.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does the query return?
AAll books with author_id 1
BThe author document with _id 1
CAll authors in the database
DNo documents
💡 Hint
Check the 'Input/Query' and 'Result/Output' columns at step 4 in execution_table.
According to variable_tracker, what is the state of the books collection after step 2?
AEmpty
BContains two book documents referencing author_id 1
CContains one book document
DContains author documents
💡 Hint
Look at the 'books collection' row under 'After Step 2' in variable_tracker.
If we did not store author_id in the books, what would be the impact on querying children by parent?
AWe could still easily find all books by author_id
BWe would need to embed the entire author document in each book
CWe could not link books to authors by a simple query
DNo impact, queries remain the same
💡 Hint
Refer to key_moments about why child documents store parent's _id.
Concept Snapshot
One-to-many referencing in MongoDB:
- Parent document has unique _id.
- Child documents store parent's _id as a reference.
- Query parent by _id.
- Query children by author_id (parent's _id).
- Keeps data normalized and avoids duplication.
Full Transcript
This visual execution shows how one-to-many referencing works in MongoDB. First, a parent document (author) is inserted with a unique _id. Then multiple child documents (books) are inserted, each storing the parent's _id in a field (author_id). When querying, we first find the parent document by its _id, then find all child documents that reference this _id. This pattern keeps data normalized, avoids duplication, and allows easy updates to the parent without changing children. The execution table traces each step: inserting documents, querying parent and children, and combining results. The variable tracker shows how collections and query results change after each step. Key moments clarify why children store only the parent's _id and how queries link them. The quiz tests understanding of these steps and the importance of referencing. This pattern is common in MongoDB for modeling one-to-many relationships.