繁体   English   中英

通过 C# MongoDB 驱动程序聚合查询结果

[英]Aggregating query results by C# MongoDB driver

MongoDB 数据库中有两个 collections。 根据以下示例,一个集合称为Department

{
    "_id": "Dept1",
    "Description": "Department 1",
    "ApplicationName": "Payroll",
    "Version": 1
}

{
    "_id": "Dept2",
    "Description": "Department 2",
    "ApplicationName": "Payroll",
    "Version": 1
}

{
    "_id": "Dept3",
    "Description": "Department 3",
    "ApplicationName": "Payroll",
    "Version": 1
}

另一个集合称为UserAssociation ,其数据类似于以下示例:

{
    "_id": "Associate1",
    "DepartmentIds": ["Dept1","Dept2"]
}

{
    "_id": "Associate2",
    "DepartmentIds": ["Dept2","Dept3"]
}

{
    "_id": "Associate3",
    "DepartmentIds": ["Dept1", "Dept2","Dept3"]
}

这两个文档的C#型号分别是:

public class Department
{
    public string Id { get; set; }
    public string Description { get; set; }
    public string ApplicationName { get; set; }
    public int Version { get; set; }
}

public class Associate
{
    public string Id { get; set; }
    public string[] DepartmentIds { get; set; }
}

我想在一天结束时得到以下 model 由聚合或“加入” (任何可以帮助更好的东西)填充:

public class DepartmentAssociation
{
    public string AssociateId { get; set; }
    public Department[] Departments { get; set; }
}

如何在 C# 中实现这个目标?

您需要将数据聚合到单个文档中,理想情况下这应该在存储点,但是如果您不能这样做,mongo 确实包含查找 function

它的语法是

$lookup:
{
    from: "<collection to join>",
    localField: "<field from the input documents>",
    foreignField: "<field from the documents of the 'from' collection>",
    as: "<output array field>"
}

或者如果你在 c# 中编写它

PipelineDefinition<BsonDocument, BsonDocument> pipeline = new BsonDocument[]
{
    new BsonDocument("$lookup", new BsonDocument(){
            {"from", "<collection to join>"},
            {"localField", "<field from the input documents>"},
            {"foreignField", "<field from the documents of the 'from' collection>"},
            {"as", "<output array field>"}
     }
};

using (var cursor = await collection.AggregateAsync(pipeline, options))
{
    while (await cursor.MoveNextAsync())
    {
        var batch = cursor.Current;
        foreach (BsonDocument document in batch)
        {
            Console.WriteLine(document.ToJson());
        }
    }
}

更具体的细节

PipelineDefinition<BsonDocument, BsonDocument> pipeline = new BsonDocument[]
{
    new BsonDocument("$lookup", new BsonDocument(){
            {"from", "Department"},
            {"localField", "DepartmentIds"},
            {"foreignField", "_id"},
            {"as", "Departments"}
     }
};

using (var cursor = await collection.AggregateAsync<DepartmentAssociation>(pipeline, options))
{
    while (await cursor.MoveNextAsync())
    {
        var batch = cursor.Current;
        foreach (DepartmentAssociation document in batch)
        {
            ...
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM