Skip to content

Commit 1a78491

Browse files
committed
agg_pipeline_merge - fixed bad code
1 parent 922e940 commit 1a78491

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

docs/50-aggregation/7-merge.mdx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,21 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
141141
<TabItem value="books" label="through books collection">
142142
<div>
143143
```csharp
144-
var pipeline = booksCollection.Aggregate()
145-
.AppendStage<BsonDocument>(new BsonDocument{"$unwind", "$authors"})
146-
.AppendStage<BsonDocument>(new BsonDocument{"$group", new BsonDocument{{"_id","$authors.name"},{"totalBooks", new BsonDocument{"$sum", 1}}}})
147-
.AppendStage<BsonDocument>(new BsonDocument{"$merge", new BsonDocument{{"into","author_stats"},{"on","_id"},{"whenMatched","merge"},{"whenNotMatched","insert"}}});
148-
149-
var result = pipeline.ToList();
144+
var result = await booksCollection.Aggregate()
145+
.Unwind("authors")
146+
.Group(new BsonDocument
147+
{
148+
{ "_id", "$authors.name" },
149+
{ "totalBooks", new BsonDocument("$sum", 1) }
150+
})
151+
.AppendStage<BsonDocument>(new BsonDocument("$merge", new BsonDocument
152+
{
153+
{ "into", "author_stats" },
154+
{ "on", "_id" },
155+
{ "whenMatched", "merge" },
156+
{ "whenNotMatched", "insert" }
157+
}))
158+
.ToListAsync();
150159
```
151160
</div>
152161
</TabItem>

0 commit comments

Comments
 (0)