MongoDb c# 2.0 驱动程序 AddToSet 方法

MongoDb c# 2.0 driver AddToSet method

我有以下代码是用 MongoDb 2.0 c# driver 实现的。但是我需要访问将要插入的 ProfileMailLists 集合。我已经在构造函数中使用 p 编写了预期的解决方案,但是如何通过多个操作来实现它?

IMongoCollection<Profile> dbCollection = DetermineCollectionName<Profile>();

var filter = Builders<Profile>.Filter.In(x => x.ID, profiles.Select(x => x.ID));

var updateMl = Builders<Profile>.Update.AddToSet(p => p.MailLists, new Profile2MailList
                        {
                            MailListId = maillistId,
                            Status = p.MailLists.MergeMailListStatuses(),
                            SubscriptionDate = DateTime.UtcNow
                        });
        
dbCollection.UpdateManyAsync(filter, updateMl, new UpdateOptions { IsUpsert = true });

我找到了以下解决方案:

IMongoCollection<Profile> dbCollection = DetermineCollectionName<Profile>();

var filter = Builders<Profile>.Filter.In(x => x.ID, profiles.Select(x => x.ID));

var profile2maillists = new List<Profile2MailList>();

foreach(var profile in profiles)
 {
     profile2maillists.Add(
         new Profile2MailList
                 {
                            MailListId = maillistId,
                            Status = profile.MailLists.MergeMailListStatuses(),
                            SubscriptionDate = DateTime.UtcNow
                 });
 }
var updateMl = Builders<Profile>.Update.AddToSetEach(p => p.MailLists, profile2maillists);
dbCollection.UpdateManyAsync(filter, updateMl, new UpdateOptions { IsUpsert = true });