Mongodb C#-仅当嵌套数组的嵌套元素符合条件时才聚合$ push

时间:2019-10-21 08:47:23

标签: c# mongodb mongodb-.net-driver

在询问之前,我已经在互联网上看了很多东西,但是我找不到解决方案...

有一个我的类和对象的例子:

课程:

public class A {
    [BsonId]
    public int _id {get; set;}
    public List<B> ListOfB {get; set;}
    public TypeA Type {get; set;}
}

public class B {
    public int _id {get; set;}
    public int PersonId {get; set;}
    public Person Person {get; set;} // This one is not save on the DB, it is retrieved through a Lookup
    public int Type {get; set;}
}

public class Person {
    [BsonId]
    public int _id {get; set;}
    public string Name {get; set;}
}

对象:

{
    "_id": 0;
    "Type", 0,
    "ListOfB": [
        {
            "_id": 0,
            "PersonId": 300,
            "Type": 0
        },
        {
            "_id": 1,
            "PersonId": 24,
            "Type": 0
        },
        {
            "_id": 2,
            "PersonId": 59,
            "Type": 1
        }
    ]
},
{
    "_id": 1;
    "Type", 1,
    "ListOfB": [
        {
            "_id": 0,
            "PersonId": 45,
            "Type": 1
        },
        {
            "_id": 1,
            "PersonId": 102,
            "Type": 0
        },
        {
            "_id": 2,
            "PersonId": 33,
            "Type": 1
        }
    ]
}
,
{
    "_id": 2;
    "Type", 1,
    "ListOfB": [
        {
            "_id": 0,
            "PersonId": 66,
            "Type": 1
        },
        {
            "_id": 1,
            "PersonId": 89,
            "Type": 0
        },
        {
            "_id": 2,
            "PersonId": 404,
            "Type": 0
        }
    ]
}

B是A集合的一部分,没有B的集合。 到目前为止,我已经做了类似的事情来检索对象:

BsonDocument groupMappingA = new BsonDocument();
groupMappingA.Add(new BsonElement("_id", "$_id"));

BsonDocument groupMappingB = new BsonDocument();
groupMappingB.Add(new BsonElement("_id", "$ListOfB._id"));
groupMappingB.Add(new BsonElement("PersonId", "$ListOfB.PersonId"));
groupMappingB.Add(new BsonElement("Person", "$ListOfB.Person"));
groupMappingB.Add(new BsonElement("Type", "$ListOfB.Type"));

groupMappingA.Add(new BsonElement("ListOfB", new BsonDocument(
                                                "$push", new BsonDocument(
                                                        "$cond", new BsonArray {
                                                            new BsonDocument("$eq", new BsonDocument("$ListOfB.Type", "0")),
                                                            new BsonDocument("$ListOfB", groupMappingB)
                                                        }
                                                )
                                            )
                                )
                );

AggregateUnwindOptions<A> unwindOptions = new AggregateUnwindOptions<A>() { PreserveNullAndEmptyArrays = true };
db.GetCollection<A>("A").Aggregate()
                        .match(x => x.Type == 1)
                        .Unwind("ListOfB", unwindOptions)
                        .Lookup("Person", "ListOfB.PersonId", "_id", "ListOfB.Person") // Here we set up Person through the ID saved in the object B
                        .Unwind("ListOfB.Person", unwindOptions)
                        .Group(groupMappingA)
                        .As<A>()
                        .ToList();

所以我想要实现的是检索所有对象A,并对所有对象B进行过滤,以仅获得类型等于0的对象,对于每个对象B,我都应该拥有Person,例如:

{
    "_id": 1;
    "Type", 1,
    "ListOfB": [
        {
            "_id": 1,
            "PersonId": 102,
            "Type": 0,
            "Person": { "_id": 102, "Name": "Billy" } 
        }
    ]
},
{
    "_id": 2;
    "Type", 1,
    "ListOfB": [
        {
            "_id": 1,
            "PersonId": 89,
            "Type": 0,
            "Person": { "_id": 89, "Name": "Finn" }
        },
        {
            "_id": 2,
            "PersonId": 404,
            "Type": 0,
            "Person": { "_id": 404, "Name": "Jake" }
        }
    ]
}

但是这行不通...我可以检索类型1的每个A对象,所有Person对象都在其B对象中,但是我得到了B的完整列表,不仅是类型0的那些。 / p>

我首先尝试在Mongodb Compass上进行操作,但是到目前为止,我没有成功,然后我在互联网上查看了内容,但是找不到我想要的东西...我开始怀疑它是否是甚至有可能在嵌套列表上获得带有过滤器的对象?

预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我找到了解决方法,我将答案发布在这里,以防有人需要。

twig:
    form_themes:
        - '@FOSCKEditor/Form/ckeditor_widget.html.twig'
fos_ck_editor:
    input_sync: true
    default_config: base_config
    configs:
        base_config:
            toolbar:
                - { name: "styles", items: ['Bold', 'Italic', 'BulletedList', 'Link'] }