0
public class Parent
    {
        [BsonId]
        public string Id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public List<Child> Children { get; set; }
    }

    public class Child
    {
        [BsonId]
        public string Id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public List<Pet> Pets { get; set; }
    }

    public class Pet
    {
        [BsonId]
        public string Id { get; set; }
        public string Name { get; set; }
    }

Inserting the collection with child and Pet

Parent parent = new Parent() { Id = ObjectId.GenerateNewId().ToString(), };

        List<Child> children = new List<Child>();
        List<Pet> pets = new List<Pet>();

        children.Add(new Child()
        {
            Id = ObjectId.GenerateNewId().ToString(),
            Firstname = "Child",
            Lastname = "One"
        });
        children.Add(new Child()
        {
            Id = ObjectId.GenerateNewId().ToString(),
            Firstname = "Child",
            Lastname = "Two"
        });
        pets.Add(new Pet() { Id = ObjectId.GenerateNewId().ToString(), Name = "Fishy" });

        parent.Children = children;
        parent.Children[0].Pets = pets;



        collection.Insert(parent);

After Inserting how can I update the name of the pet to doggy ????

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Have you searched for similar questions here? Try to read answers from questions which you can see on right side of this page. http://stackoverflow.com/questions/8092552/update-embedded-document-in-mongodb-using-c-sharp?rq=1 for example. – FLCL Jun 12 '14 at 07:20
  • Thanks for ur reply FLCL i have seen all the other posts but nothing worked out, the problem is i am able to update the child of a document but i need to update the sub child – user3732901 Jun 13 '14 at 09:54

1 Answers1

1

Unfortunately, as described there, MongoDb does not provide the feature you need. It's very sad for document-oriented database, as I think(but it may be appear in the next versions).

So the only why is to load document and update them like this:

var res = col.Find(Query.ElemMatch("Children.Pets", Query.EQ("Name", "Fishy")));
foreach(var _parent in res)
{
    foreach (var _child in _parent.Children)
    {
        var pets = _child.Pets;
        if(pets!=null)
        {
             var pet = pets.FirstOrDefault(x => x.Name == "Fishy");
             if(pet!=null)
                 pet.Name = "Doggy";
        }
    }
    col.Save(_parent);
}
FLCL
  • 2,445
  • 2
  • 24
  • 45
  • Even this is of no use FLCL i am getting null reference exception at var pet = _child.Pets.SingleOrDefault(x => x.Name == "Fishy"); line – user3732901 Jun 14 '14 at 10:23
  • you also have to debug ur code to catch the reason of the exceptions - this is regular thing what every programmer does – FLCL Jun 15 '14 at 21:53
  • I've searched two whole days for this answer... I was getting desparate because my grandchildren never changed... +1 – LiliumCandidum Aug 01 '22 at 09:53
  • @LiliumCandidum You can also check https://stackoverflow.com/questions/18173482/mongodb-update-deeply-nested-subdocument answers, looks like MongoDB authors added more options to handle this. gl! – FLCL Aug 01 '22 at 16:08