1

I started to use mongodb instead of SQL databases in my new projects. And I have a lot of questions how and when use mongodb instead of sqldb. In my new project I want to have user and projects,categories(and many many more) per users. And if is better to create one colllection 'users' and add there project,categries e.g.

[
    {
        userName: 'John',
        products: [
            {
                productName: 'Product'
            },
            {
                productName: 'Product2'
            },
            {
                productName: 'Product3'
            },
            {
                productName: 'Product4'
            }
        ],
        categories: [
            {
                categoryName: 'Category'
            },
            {
                categoryName: 'Category2'
            },
            {
                categoryName: 'Category3'
            },
            {
                categoryName: 'Category4'
            }
        ]
    }
]

Or create products and categories collections and relation to user? If it comes to the second way do you have any tutorials about relations in mongodb? Or any tutorials which can teach me how to change my thinking about databases? Because I dont know how to build data structure for nosql databases.

bartekgorski
  • 124
  • 2
  • 7
  • Look at things like [Mongoose populate vs object nesting](https://stackoverflow.com/a/24096822/2313887) or [MongoDB relationships: embed or reference?](https://stackoverflow.com/a/5373969/2313887). "Which is Best?" is not someone elses choice/opinion, but can only be determined by your own application usage patterns, i.e "How it uses the data". The general reality though is that MongoDB came into existence in a space where it was generally trying to "avoid" relations, but there are of course other things to consider. – Neil Lunn Aug 04 '17 at 22:32
  • So generally the only answers to this ( other than the existing general overviews ) are basically opinion based. And asking for tutorial recommendations here is also not within the site guidelines. Reading those two should give an indication of what to look for. As for the "multiple" arrays in your storage, I would advise against that. At most have **one** array only and differentiate the "type" as either "category" or "product". The reasoning is explained within the [limitations of multikey-indexes](https://docs.mongodb.com/manual/core/index-multikey/#limitations). – Neil Lunn Aug 04 '17 at 22:36

0 Answers0