Mongodb c#driver - AggregateToCollectionOperation

发布于 2025-01-13 06:30:23 字数 953 浏览 3 评论 0原文

我正在使用库 MongoDB.Driver 2.8.1 (我计划升级到最新版本,这是一个旧项目)。

我试图在我的代码中使用运算符 $out 进行查询,所以我编写了这段代码(这不是很好,但这不是问题)

await collectionAcc.Aggregate().AppendStage<BsonDocument>(@"{
                    $unwind: {
                        path: ""$Packs"",
                        preserveNullAndEmptyArrays: false
                    }
                }")
            .AppendStage<BsonDocument>(@"{
                    $replaceRoot: {
                                 newRoot: { $mergeObjects: [ ""$$ROOT"", {""Sku"" : ""$Packs.Sku"" }, {""SizePack"" : ""$Packs.Size"" } ] }
                            }
                }")
            .AppendStage<BsonDocument>(@"{ $out: ""accessoryPacks"" }").FirstOrDefaultAsync();

我运行它并出现此错误: “AggregateOperation 的管道包含一个 $out 运算符。请使用 AggregateOutputToCollectionOperation 代替”

很好,我尝试使用 AggregateOutputToCollectionOperation 类,但我根本没有找到任何文档,也没有一个像样的示例。

有人可以帮助我吗?

I'm using the library MongoDB.Driver 2.8.1 (i'm planning to upgrade to the latest version, it's an old project).

I'm trying to use the operator $out for a query in my code so I wrote this piece of code (it's not wonderful, but it's not a problem)

await collectionAcc.Aggregate().AppendStage<BsonDocument>(@"{
                    $unwind: {
                        path: ""$Packs"",
                        preserveNullAndEmptyArrays: false
                    }
                }")
            .AppendStage<BsonDocument>(@"{
                    $replaceRoot: {
                                 newRoot: { $mergeObjects: [ ""$ROOT"", {""Sku"" : ""$Packs.Sku"" }, {""SizePack"" : ""$Packs.Size"" } ] }
                            }
                }")
            .AppendStage<BsonDocument>(@"{ $out: ""accessoryPacks"" }").FirstOrDefaultAsync();

I run it and this error appear:
"The pipeline for an AggregateOperation contains a $out operator. Use AggregateOutputToCollectionOperation instead"

Good, I try to use AggregateOutputToCollectionOperation class but I found no documentation at all, nor a decent example.

Someone can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眸 2025-01-20 06:30:23

哦,我错过了您调用 FirstOrDefault 的事情。 $out$merge 应该是管道中的最后阶段,但 FirstOrDefault 在它们之后另外添加了 { limit : 1 } 阶段。您应该在 $out 之前在管道内添加此阶段

oh, I missed that you call FirstOrDefault. $out and $merge should be the last stages in pipeline, but FirstOrDefault additionally adds { limit : 1 } stage after them. You should add this stage inside pipeline before $out

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文