Reputation: 4260
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#?
Upvotes: 0
Views: 1843
Reputation: 5500
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)
{
...
}
}
}
Upvotes: 2