$addToSet accumulator for unique arrays
📖 Scenario: You are managing a database for a book club. Each member can borrow multiple books, and you want to keep track of all unique book titles borrowed by each member.
🎯 Goal: Build an aggregation pipeline that groups the data by member and collects a unique list of all book titles they have borrowed using the $addToSet accumulator.
📋 What You'll Learn
Create a collection named
borrowedBooks with documents containing member and bookTitle fields.Add a configuration variable for the aggregation pipeline stages.
Write an aggregation query that groups by
member and uses $addToSet to collect unique bookTitle values.Complete the aggregation pipeline with a final stage to format the output.
💡 Why This Matters
🌍 Real World
Tracking unique items borrowed or purchased by users is common in libraries, stores, and online platforms.
💼 Career
Understanding $addToSet helps in writing efficient aggregation queries for data analysis and reporting in MongoDB.
Progress0 / 4 steps