public static async Task TestAsync()
|
{
|
var client = new MongoClient();
|
var db = client.GetDatabase("test");
|
var col = db.GetCollection<User>("users");
|
|
await db.DropCollectionAsync("users");
|
|
await col.InsertOneAsync(new User
|
{
|
Id = "1",
|
AppIntegrations = new[]
|
{
|
new UserAppIntegration { AppName = "App1", CanRemove = true },
|
new UserAppIntegration { AppName = "App2", CanRemove = false },
|
}
|
});
|
|
var pipeline = col.Aggregate()
|
.Match(u => u.Id == "1")
|
.Project(u => u.AppIntegrations.Select(i => new UserAppIntegrationStatus { AppName = i.AppName, CanRemove = !i.CanRemove }));
|
|
var results = await pipeline.ToListAsync();
|
|
//warm up
|
|
Console.ReadKey();
|
}
|
|
public class User
|
{
|
public User()
|
{
|
AppIntegrations = new UserAppIntegration[0];
|
}
|
|
public string Id { get; set; }
|
public string Email { get; set; }
|
public UserAppIntegration[] AppIntegrations { get; set; }
|
}
|
|
public class UserAppIntegration
|
{
|
public string AppName { get; set; }
|
public bool CanRemove { get; set; }
|
}
|
|
public class UserAppIntegrationStatus
|
{
|
public string AppName { get; set; }
|
public bool CanRemove { get; set; }
|
}
|