0

Let's have following simplified construct. For example some family-tree-like-structure. For simplicity, it is guaranteed all names are unique.:

{
_id: "abc",
name: "Alan",
children: [
    {name: "Sophia", children: []},
    {name: "Bruno", children: [
        {name: "Ivan", children: [
            {name: "Maya", children: []}
        ]}
    ]}
]

}

When I need to change name of "Ivan" to "Igor" and I only know the top document's id (_id = "abc"), while I don't know, how nested Ivan exactly is, is it still posible to make the update without loading entire document (in my case of size of megabytes) to client application, modification of entire object and replacing entire object back to mongoDb?

Thanks for any advice.

ooouuiii
  • 289
  • 3
  • 14
  • You can use `$` positional operator here – Ashh Aug 26 '18 at 14:38
  • I understand the positioning operator so that I can use it for list (all documents on the same nesting level) but here can be the searched "element" once at children.0, and other time on children.1.children.0.children.1.children.50.children.1. Can the $ operator handle it? – ooouuiii Aug 26 '18 at 14:59
  • Then you can use arrayFilters for that but even for that you must know the nested levels of the array – Ashh Aug 26 '18 at 15:07
  • 2
    I think, this is a sign you that you should reconsider your document structure. I would suggest you structure them like in this [question](https://stackoverflow.com/questions/40526148/recursive-search-on-a-collection-in-mongodb) – styvane Aug 26 '18 at 16:17
  • Wow, I've missed $graphLookup somehow. Actually I have currently very similar, normalized structure. Nothing is nested, all is referenced by parent_id and slug. And it works well. But the solution needs to scale heavily and since there will be much more readings then writes and it needs to be sharded, I decided to go with embeded documents (not sure about it still). I thought there would be some workaround in a sense I would have some mongo-extension method on db server and it would change the document, without need to transfer it to client app. – ooouuiii Aug 26 '18 at 17:53

0 Answers0