简体   繁体   中英

Aggregating query results by C# MongoDB driver

There are two collections in a MongoDB database. One collection is called Department per the example below:

{
    "_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
}

The other collection is called UserAssociation and its data looks like the following example:

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

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

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

The C# models of these two documents are:

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; }
}

I'd like to get the following model populated by aggregation or "join" (anything that can help better) at the end of the day:

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

How can I achieve this goal in C#?

you need to aggregate the data into a single document, ideally this should be at the point of storage, however if you can't do this mongo does include a lookup function

the syntax for it are

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

or if you compose it in 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());
        }
    }
}

in more specific detail

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)
        {
            ...
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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