Replace client-side join with MongoDB aggregation pipeline in Lab 1.5#108
Replace client-side join with MongoDB aggregation pipeline in Lab 1.5#108
Conversation
Co-authored-by: tibitoth <8333960+tibitoth@users.noreply.github.com>
docs/en/seminar/mongodb/index.md
Outdated
| // 1.5 | ||
| // 1.5 - Recommended solution with aggregation pipeline | ||
| Console.WriteLine("\t1.5:"); | ||
| var pipeline = new[] |
There was a problem hiding this comment.
@copilot this does not have any build errors but it returns 0 results. I do not have any clue why.
There was a problem hiding this comment.
Fixed in commit e0823ad. The issue was using Pascal case field names ($product.Name, $product.Stock) instead of lowercase ($product.name, $product.stock). The $lookup operator returns raw MongoDB documents which use lowercase field names, not the C# entity mapped properties.
There was a problem hiding this comment.
@copilot az orders kollekcióban az OrderItems helyesen orderItems kellene, hogy legyen, és ezen belül pedig a ProductID is helyesen productID kellene legyen.
There was a problem hiding this comment.
Fixed in commit cf199ab. Changed $OrderItems to $orderItems and $OrderItems.ProductID to $orderItems.productID to match the MongoDB schema's camelCase field names.
There was a problem hiding this comment.
@copilot now it is correct but did not use the helper methods from Monodb,driver package. We can use this instead:
var results = ordersCollection
.Aggregate()
.Unwind(o => o.OrderItems)
.Group(
new BsonDocument
{
{ "_id", "$orderItems.productID" },
{ "orderCount", new BsonDocument("$sum", 1) }
})
.Match(g => g["orderCount"] >= 2)
.Lookup<BsonDocument, BsonDocument>("products", "_id", "_id", "product")
.Unwind("product")
.Project(d => new
{
productName = d["product"]["name"],
stock = d["product"]["stock"],
orderCount = d["orderCount"]
})
.ToList();This seems better but could you find and more improvements to avoid BsonDocuments and use more type checks?
There was a problem hiding this comment.
Updated in commit b50c2aa to use the MongoDB driver's fluent API methods (.Aggregate().Unwind().Group().Match().Lookup().Project()) instead of constructing a BsonDocument array. This is more idiomatic and consistent with exercise 1.3's style.
Co-authored-by: tibitoth <8333960+tibitoth@users.noreply.github.com>
Co-authored-by: tibitoth <8333960+tibitoth@users.noreply.github.com>
Co-authored-by: tibitoth <8333960+tibitoth@users.noreply.github.com>
MongoDB Lab 1.5 Memory Optimization - Complete
Successfully addressed the issue "Mongodb Lab 1.5 memóriában csinál mindent" by providing an optimized solution using MongoDB's aggregation pipeline.
Changes Completed:
Solution Overview:
Recommended approach (NEW): MongoDB aggregation pipeline with
$lookup.Aggregate().Unwind().Group().Match().Lookup().Project()Alternative approach (ORIGINAL): Client-side join
Implementation Details:
The solution now uses the MongoDB driver's fluent API methods:
.Aggregate()- starts the aggregation pipeline.Unwind(o => o.OrderItems)- unwinds the order items array.Group()- groups by productID and counts occurrences.Match()- filters products ordered at least twice.Lookup()- joins with products collection.Unwind()- unwinds the product array result.Project()- selects required fieldsBug Fixes:
product.name,product.stock(lowercase)orderItemsandorderItems.productID(camelCase)Key Benefits:
✅ Memory efficient - doesn't load all data into memory
✅ Server-side processing - leverages MongoDB indexes
✅ Scalable - works with large databases
✅ Idiomatic - uses MongoDB driver's fluent API
✅ Readable - chained method calls instead of array of BsonDocuments
✅ Consistent - matches style in exercise 1.3
✅ Educational - shows both approaches with clear guidance
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.