c# - MongoDb c# 2.0 驱动程序 AddToSet 方法

标签 c# mongodb mongodb-.net-driver mongodb-csharp-2.0

我有以下代码,它是使用 MongoDb 2.0 c# 驱动程序实现的。但我需要访问将插入的 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 });

关于c# - MongoDb c# 2.0 驱动程序 AddToSet 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31877918/

相关文章:

MongoDB被勒索赎金

c# - 使用官方 c# 驱动程序在 mongodb 中保存具有双向关系的对象

c# - 在 C# 中使用接口(interface)有哪些优点?

c# - 如何更改此设计以避免沮丧?

c# - 无法理解为什么 NerdDinner MVC 返回错误

linux - 为什么MongoDB在Ubuntu 11.10安装时无法创建用户?

node.js - 如何对两个日期之间的字段进行计数和求和?

c# - 使用 SSH.NET 在进度栏中显示文件下载进度

c# - mongodb C# 驱动程序更新多个字段

mongodb-.net-driver - 有没有办法从 MongoDB 检索没有 _id 字段的数据?