2

I have a Profiles document collection with array of the following documents :

public class Profile2MailList
{


    [BsonElement(elementName: "listId")]
    [BsonRequired]
    public int MailListId;

    [BsonElement(elementName: "status")]
    [BsonRequired]
    public int Status;

    [BsonElement(elementName: "subscriptionDate")]
    [BsonRequired]
    public DateTime SubscriptionDate;
} 

in each Profile. I need to add to the Profile2MailList array a new Profile2MailList document in each Profile based on Profile2MailList which already contains in a certain Profile. So i need to

  • Take needed profiles from Profiles collection
  • Update Profile2Maillist array in each Profile
  • Run update command How can i perform that action via C# 2.0 MongoDb Driver. I have MongoDb v 3.0.2. I try to make it by the following way :

       List<Profile> listProfiles = new List<Profile>();
                foreach (Profile item in profiles)
                {
                    item.MailLists.ToList().Add(new Profile2MailList(maillistId, item.MailLists.FirstOrDefault().Status));
                    var t = item;
                    listProfiles.Add(t);
                }
        dbCollection.UpdateManyAsync(listProfiles)
    
Vladyslav Furdak
  • 1,765
  • 2
  • 22
  • 46
  • I know the changes in the new C# driver are poorly documented, but you must have at least tried something. Please show your attempt at this ( even if it fails ) as without it this just looks like a "write my code for me" question. Try and fail is acceptable, and it at least shows clearly what you want to do. – Blakes Seven Sep 18 '15 at 10:03
  • @BlakesSeven i have no idea how to do it, i'm able to use `InsertManyAsync` in case when i need to insert new documents - it works fine, but how to update many documents with complex update scenario ? I'm able to perform `UpdateOneAsync` but `UpdateDefinition` doesn't cover my needs – Vladyslav Furdak Sep 18 '15 at 13:15
  • Great big [edit](http://stackoverflow.com/posts/32649188/edit) link on your question. Show your failed attempts. Believe me that no-one will laugh. But no effort gains no help from me. – Blakes Seven Sep 18 '15 at 13:21
  • @BlakesSeven i've added additional information – Vladyslav Furdak Sep 18 '15 at 15:06

1 Answers1

3

The method "UpdateManyAsync" only works if you want to perform one type of update, which doesn't seem to be your case. What you want to do is perform a Bulk-Update instead. Building on your example, you would want to do something like:

        var bulk = new List<WriteModel<Profile>>();
        foreach (Profile item in profiles)
        {
            var newProfile = new Profile2MailList(maillistId, item.MailLists.FirstOrDefault().Status);
            var filter = Builders<Profile>.Filter.Eq(g => g.Id, item.Id);
            var update = Builders<Profile>.Update.AddToSet(item.MailLists, newProfile);
            var updatemodel = new UpdateOneModel<Profile>(filter, update);

            bulk.Add(updatemodel);  
        }
        await profileCollection.BulkWriteAsync(bulk);

The AddToSet operator adds a value to an array unless the value is already present.

Philip
  • 3,135
  • 2
  • 29
  • 43