using System;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.Serialization.Attributes;
public abstract class MongoModelBase
{
[BsonId]
public ObjectId Id { get; set; }
}
[BsonDiscriminator(Required = true, RootClass = true)]
[BsonKnownTypes(typeof(OrganizationActivityLog), typeof(ShipmentActivityLog))]
public abstract class ActivityLogBase : MongoModelBase
{
public string ActivityDescription { get; set; }
}
public class ShipmentActivityLog : ActivityLogBase
{
public string shipmentId { get; set; }
}
public class OrganizationActivityLog : ActivityLogBase
{
public int OrganizationId { get; set; }
}
class Program
{
static void Main(string[] args)
{
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("test");
var collection = database.GetCollection<ActivityLogBase>("activitylogs");
var filterBuilder = Builders<OrganizationActivityLog>.Filter;
var result = collection.Aggregate()
.OfType<OrganizationActivityLog>()
.Group(
e => e.OrganizationId,
e => new {
ActivityLog = e.Select(f => new OrganizationActivityLog
{
Id = f.Id,
OrganizationId = f.OrganizationId,
ActivityDescription = f.ActivityDescription
}).First()
}
).ToList();
Console.WriteLine(result);
}
}