MongoDB - children and parent structure

前端 未结 2 1868
余生分开走
余生分开走 2021-02-01 10:53

Having just recently delved into the world of NoSQL with MongoDB, I am still struggling to understand the best approach to architecture without 3rd normalizing the data and then

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 11:40

    Very nice summary from @CynicalProgrammer, I would add one more: use the fact that json is a tree to your adventage! No need to store only the 'left' and 'right' node ID in each node, why not store a subtree, 3-4-5 deep down? So it would look like this:

    {
    left: {
        left: {
            left: {...},
            right: {...}
        },
        right: {...}
    },
    right: {... you get the idea ...}
    }
    

    This way, you'd need hell of a lot less queries for traverse the tree, and the number of documents in the collection would be a fraction. A slight drawback is that you need somewhat more complex code to write to the tree, and mongodb documents will be bigger, meaning individual writes are slower.

    I think this is probably the best way to store trees in mongodb. But again, remember, mongo is meant for document storage, not "big-ass schemaless tree" storage. You might want to look into something like neo4j for that.

提交回复
热议问题