2

我有以下使用MongoDb 2.0 c# driver实现的代码。但是我需要访问将被插入的MailLists集合。Profile我已经在构造函数中使用了预期的解决方案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 });
4

1 回答 1

4

我找到了以下解决方案:

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 });
于 2015-08-07T14:07:40.310 回答