massimilianokraus / MongoDB.DeepUpdater.CSharp

An extension of the official framework of MongoDB for C#
MIT License
6 stars 1 forks source link

How to replace and delete array elements? #1

Closed softwarea closed 3 years ago

softwarea commented 3 years ago

Massimiliano,

I hope you still look in here and again.

I think your "DeepUpdater" is wonderful because it allows me to finally work "type-safe" with my deep arrays. Thanks so much. This is a huge help.

There are only two things I can't really get solved:

  1. AddToSet() adds a new element to an array. Cool.

But what if I would like to update/replace an existing array element? Please see the code snippet below. What if I would like to replace "Ball" with "Rubber Chicken"? Does your fluent API support such updates? Or even better: does it support "upserts"? Add if the element does not exist yet, replace if it does.

  1. How can I delete a concrete array element? I found PopFirst() and PopLast, but I am rather looking for Pop(toy => toy.Id == 17);

`var addToyOperation = Builders.Update .Deep(mike) // Suppose that 'mike' is the Person to update .SelectArray(x => x.Pets) .Where(x => x.Type == "Dog") .SelectArray(x => x.Toys) .AddToSet(new Toy { Id = 17, Name = "Ball" });

_mongoCollection.UpdateOne(filter, addToyOperation);`

I hope you can help me out. Thanks so much! Ingmar

massimilianokraus commented 3 years ago

Thank you very much for your interest :)

You issue made me revisit my library a bit, updating the packages, etc. I'm working on it, It will take a couple of weeks. Meantime, I can answer the "delete" question: you can you PullAll(). PullAll() takes the instances of the elements to be removed from the array, and you can use a LINQ query to obtain those elements. For example:

.SelectArray(x => x.Pets)
.PullAll(mike.Pets.Where(x => x.Type == "Dog"))

Clearly, if the array is a bit nested, creating the LINQ query to those elements is annoying and also it would be a repetition of what is already in the Update methods... I should simplify that part too, to obtain what you proposed.

I will update this thread as soon as I push these changes.

softwarea commented 3 years ago

Hi Massimiliano,

just checking in. Any progress so far? Would be great to have an updated extension soon.

If you are still working on this, do you think you could implement an UPSERT as well? In other words: add, if not existent, replace if existent?

And please let me compensate you for your work and help somehow.

Thanks!

Ingmar

softwarea GmbH | Untere Bachgasse 12 | 93047 Regensburg | +49 941 38223880 Festnetz | +49 179 5044534 Mobil | Geschäftsführer: Ingmar Bode | Handelsregister Regensburg | Registernummer HRB 9601 | USt-IdNr. DE813671417

------ Original Message ------ From: "Massimiliano Kraus" notifications@github.com To: "massimilianokraus/MongoDB.DeepUpdater.CSharp" MongoDB.DeepUpdater.CSharp@noreply.github.com Cc: "softwarea" github@softwarea.de; "Author" author@noreply.github.com Sent: 11.01.2021 09:41:20 Subject: Re: [massimilianokraus/MongoDB.DeepUpdater.CSharp] How to replace and delete array elements? (#1)

Thank you very much for your interest :)

You issue made me revisit my library a bit, updating the packages, etc. I'm working on it, It will take a couple of days. Meantime, I can answer the "delete" question: you can you PullAll(). PullAll() takes the instances of the elements to be removed from the array, and you can use a LINQ query to obtain those elements. For example:

.SelectArray(x => x.Pets) .PullAll(mike.Pets.Where(x => x.Type == "Dog"))

Clearly, if the array is a bit nested, creating the LINQ query to those elements is annoying and also it would be a repetition of what is already in the Update methods... I should simplify that part too, to obtain what you proposed.

I will update this thread as soon as I push these changes.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/massimilianokraus/MongoDB.DeepUpdater.CSharp/issues/1#issuecomment-757728337, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJWV7Y4KRR4HVHCVGIKTE3SZK2TBANCNFSM4V3TJUAQ.

massimilianokraus commented 3 years ago

Hi, I'm passing hard days at work: we're going in production with a new software ecosystem, you can imagine the workload I'm having. I've already fixed a couple of things on the MongoDB library, I'm planning to finish it in the weekend. I'll keep this thread updated.

softwarea commented 3 years ago

Hey Massimiliano,

haha, same for me. Monday is deadline and I am coding like hell. That's why I am kind of interested in a "fast" solution for your library. But I can find workarounds, so no worries or pressure :)

I wish you luck!!

Ingmar

Hi, I'm passing hard days at work: we're going in production with a new software ecosystem, you can imagine the workload I'm having. I've already fixed a couple of things on the MongoDB library, I'm planning to finish it in the weekend. I'll keep this thread updated.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/massimilianokraus/MongoDB.DeepUpdater.CSharp/issues/1#issuecomment-763792191, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJWV73RXL5EAK4E3X27ZFDS24EPRANCNFSM4V3TJUAQ.

massimilianokraus commented 3 years ago

Hi. I won't be able to dedicate to this project, at least for the next months. If I have the time, today I'll load a new version that uses the upgraded versions of .NET and MongoDB, but that's it. I'm sorry. Anyway, I'm willing to accept pull requests with whatever improvement. Kind regards