MongoDb c# update array in document

10,923

Working sample code for appending a list of strings to an existing list of strings using a strongly-typed Push:

class Event
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
    public List<string> Participants { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        MongoClient client = new MongoClient("mongodb://localhost/test");
        var db = client.GetServer().GetDatabase("test");
        var collection = db.GetCollection("events");
        var event0 = new Event { Name = "Birthday Party", 
            Participants = new List<string> { "Jane Fonda" } };
        collection.Insert(event0);
        collection.Update(Query.EQ("_id", event0.Id),
            Update<Event>.PushAll(p => p.Participants, 
                new List<string> { "John Doe", "Michael Rogers" }));
    }
}
Share:
10,923

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have the following code:

    _eventInstanceRepository.GetInnerCollection().Update(
                                  Query.EQ("_id", listingEventModel.Id),
                                  Update.PushWrapped<string[]>("ArtistIds", ids.ToArray()));
    

    Which is designed to update the following document:

    public class ListingEvent
    {       
        public ObjectId Id { get; set; }
        public string[] ArtistIds { get; set; }        
    }
    

    ids is a List

    Any ideas why this isn't updating the docs?

    [UPDATE]

    Also tried this!

    foreach (var id in ids)
    {
        _eventInstanceRepository.GetInnerCollection().Update(
                                  Query.EQ("_id", listingEventModel.Id),
                                  Update.Push("ArtistIds", id));
    }
    

    No luck...

    [UPDATE]

    Going back to RavenDb - at least for now. I don't see how MongoDb is a viable option the whole time there are no real sources discussing (slightly more complex than flat structure) document updates on the internet and the examples I can find simply do not work.

    [UPDATE]

    Here is the repository code:

    public class Repository<T> : IRepository<T>
    {
        private readonly MongoCollection<T> _docs;
    
        public Repository(MongoCollection<T> docs)
        {
            _docs = docs;
        }
    
        public IList<T> GetAll()
        {
            return _docs.FindAll().Select<T, T>(x => x.As<T>()).ToList();
        }
    
        //HACK!
        public MongoCollection<T> GetInnerCollection(){
            return _docs;
        }
    
        public void Save(T doc)
        {
            _docs.Save(doc);
        }
    
        public void Save(IEnumerable<T> docsToSave)
        {
            foreach (var doc in docsToSave) Save(doc);
        }
    
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    
        public void Delete(string id)
        {
            var query = Query.EQ("_id", id);
            _docs.Remove(query);
        }
    }