6

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);
    }
}
3
  • 1
    what is GetInnerCollection - you're not giving us all the code... Commented Sep 10, 2013 at 12:22
  • 1
    That literally just gets the inner collection - I've updated with the repository.cs code - it was a hack so that I could try and figure this problem out. Commented Sep 10, 2013 at 12:39
  • 1
    Side note: The GetAll method will read the entire collection in one go and it might have to use linq to objects to do... nothing. Do MongoDatabase db; db.GetCollection<T>(name).FindAll() instead. That gives you a MongoCursor<T> which implements IEnumerable<T>. Convert that to ToList if you have to, but always use SetLimit on the cursor. Commented Sep 10, 2013 at 12:44

1 Answer 1

2

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" }));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

PushAll was the one I didn't try!

Your Answer

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