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)
    
4
  • 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. Commented Sep 18, 2015 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 Commented Sep 18, 2015 at 13:15
  • Great big edit link on your question. Show your failed attempts. Believe me that no-one will laugh. But no effort gains no help from me. Commented Sep 18, 2015 at 13:21
  • @BlakesSeven i've added additional information Commented Sep 18, 2015 at 15:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.