Pedro Vallese
Pedro Vallese

Reputation: 11

MongoDB design of a database

So, I'm designing the model for the documents that I'll insert in my database and I have a question about the design. Is it better to just insert more documents in my collections or fewer nested documents? Example:

sale:{
  store_id : "2",
  vendor_id: "2,
  points : 100
}


sale:{
  store_id : "2",
  vendor_id: "2,
  points : 100
}


sale:{
  store_id : "2",
  vendor_id: "2,
  points : 100
}

sale:{
  store_id : "4",
  vendor_id: "3,
  points : 100
}

sale:{
  store_id : "4",
  vendor_id: "1,
  points : 100
}

So,in this not nested example if I have N sales, I'll have N sales, inside my collections. But if I try to nest, my example will be:

stores:{ [
  store_id : "2" 
  vendor : [
      vendor_id : "2"
      sales : [
        points : 100
      ],
      [ 
        points : 100
      ],
      [
        points : 100
      ]
  ]
],
[
  store_id: 4
  vendor : [
    vendor_id : 3
    sales : [
      point : 100
    ]
  ],
  [
    vendor_id : 1
    sale : [
      point : 100
    ]
  ]
] }; 

In this example, I nest all my sales. So, my question is: to create reports and analyze data, which one is faster? If I want to see which store sold more for example, will it be faster to analyze nested documents or one line documents?

Thank you in advance.

Upvotes: 0

Views: 148

Answers (1)

cEeNiKc
cEeNiKc

Reputation: 1318

The answer is pretty simple. If you know there are gonna be an a lot of sales and its not limited number, you have to go for a separate collection for sales. Mongodb is designed to perform amazingly fast even if there are a million documents in a collection but interestingly you are gonna face a lot of issues by nesting. Also there is a 16mb document size limit in mongodb, so eventually after a while your one store document will reach that limit and it will make things pretty ugly. It’s quite straight that you should go for a separate collection.

You can also read this blog and it will clear things out for you https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1

Upvotes: 1

Related Questions