首页 > 解决方案 > 如何使用嵌套和/或条件构建 mongo 聚合管道?

问题描述

我正在尝试构建一个聚合管道,该管道使用AND(OR(AND)条件过滤掉文档。

我需要的是类似

if (A == "A" && B == "B" && (C == NULL || (C != NULL && C == "C")))

我的查询如下所示。

内部部分:

var x = new BsonDocument
                {
                    {
                        "$or",
                        new BsonArray
                        {
                            new BsonDocument {{"C", BsonNull.Value}},
                            new BsonDocument
                            {
                                {
                                    "$and",
                                    new BsonArray
                                    {
                                        new BsonDocument
                                        {
                                            {
                                                "C",
                                                new BsonDocument {{"$ne", BsonNull.Value}}
                                            }
                                        },
                                        new BsonDocument {{"C", C}}
                                    }
                                }
                            }
                        }
                    }
                };

那么全过滤器就是

var filter = new BsonDocument
            {
                {
                    "$and",
                    new BsonArray
                    {
                        new BsonDocument {{"A", A}},
                        new BsonDocument {{"B", B}},
                        x
                    }
                }
            };

然后我这样称呼它:

var y = await collection.Aggregate().Match(filter).ToListAsync();

但是,它虽然应该返回任何结果。

在我的调试器中,最终的 Bson 看起来像:

{
    {
        "$and": [
            {
                "A": "A"
            },
            {
                "B": "B"
            },
            {
                "$or": [
                    {
                        "C": null
                    },
                    {
                        "$and": [
                            {
                                "C": {
                                    "$ne": null
                                }
                            },
                            {
                                "C": "C"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

标签: c#mongodbmongodb-query

解决方案


设法弄清楚了。我更改了查询以将其构建为匹配项,因为使用 mongo shell 更容易测试:

var match = new BsonDocument
{
    {
        "$match",
        new BsonDocument
        {
            {
                "$and",
                new BsonArray
                {
                    new BsonDocument {{"A", A}},
                    new BsonDocument {{"B", B}},
                    new BsonDocument
                    {
                        {
                            "$or",
                            new BsonArray
                            {
                                new BsonDocument {{C, BsonNull.Value}},
                                new BsonDocument
                                {
                                    {
                                        "$and",
                                        new BsonArray
                                        {
                                            new BsonDocument
                                            {
                                                {
                                                    "C",
                                                    new BsonDocument {{"$ne", BsonNull.Value}}
                                                }
                                            },
                                            new BsonDocument {{"C", C}}
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
};

希望这可以帮助任何遇到类似查询问题的人。


推荐阅读