Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Practical MongoDB Aggregations

You're reading from  Practical MongoDB Aggregations

Product type Book
Published in Sep 2023
Publisher Packt
ISBN-13 9781835080641
Pages 312 pages
Edition 1st Edition
Languages
Author (1):
Paul Done Paul Done
Profile icon Paul Done

Table of Contents (20) Chapters

Preface 1. Chapter 1: MongoDB Aggregations Explained 2. Part 1: Guiding Tips and Principles
3. Chapter 2: Optimizing Pipelines for Productivity 4. Chapter 3: Optimizing Pipelines for Performance 5. Chapter 4: Harnessing the Power of Expressions 6. Chapter 5: Optimizing Pipelines for Sharded Clusters 7. Part 2: Aggregations by Example
8. Chapter 6: Foundational Examples: Filtering, Grouping, and Unwinding 9. Chapter 7: Joining Data Examples 10. Chapter 8: Fixing and Generating Data Examples 11. Chapter 9: Trend Analysis Examples 12. Chapter 10: Securing Data Examples 13. Chapter 11: Time-Series Examples 14. Chapter 12: Array Manipulation Examples 15. Chapter 13: Full-Text Search Examples 16. Afterword
17. Index 18. Other books you may enjoy Appendix

Multi-field join and one-to-many

The previous example illustrated how to solve a one-to-one join. However, you may need to perform a one-to-many join, where a document in the first collection maps to potentially many records in the second collection. Here, you will learn how to achieve this in a pipeline.

Scenario

You want to generate a report to list all the orders made for each product in 2020. To achieve this, you need to take a shop's products collection and join each product record to all its orders stored in an orders collection. There is a one-to-many relationship between both collections, based on a match of two fields on each side. Rather than joining on a single field such as product_id (which doesn't exist in this dataset), you need to use two common fields to join (product_name and product_variation).

Note

The requirement to perform a one-to-many join does not mandate the need to join the two collections by multiple fields on each side. However, in...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}