1

I've to store line items in the database.

Currently, I'm creating a batch of them and storing it as a single document.

date: null,
items: [
    { name: 'Bla', cost: 5, ref: 'user id' },
    { name: 'Bla', cost: 5, ref: 'user id' }, 
]

But I'm having a gut feeling that it will be more costly (performance and processing) because it is an array of object. ($unwind, $project).

Will it be good if I store each line item as a document instead?

The reason why I've not done so far is we may have around 1 billion documents in 1-3 years.

Once more thing, how can I generate a unique ID for each line item? I know Mongo generates _id, but I mean a number 0, 1, 2, etc. which can be shown in invoices.

So, I'm not sure if MongoDB can handle it effectively.

Best, Faheem

EDIT:

These are the reasons that I think this question is not a duplicate to the suggested duplicates.

Those questions are referring to ref vs embedding the document. I'm not.

The question is what will be the best way (in terms of performance and low latency) to store inline items, in a batch (array of object) or as a single document. I'm not concerned about references or anything here.

Faheem
  • 1,105
  • 11
  • 28
  • 2
    *"Will it be good if I store each line item as a document instead?"* - Your words, and that is exactly what the two linked places with existing answers address. In all honesty the general topic if *primarily opinion based* other than that you will find is that the higher scoring answers out those existing ones basically point out the **facts** regarding the differences of two approaches. *"Which should you choose?"* is really up to you to decide based on the data usage patterns of your application. Which is exactly what those answers describe. Nothing new to add here. – Neil Lunn Apr 20 '19 at 05:06
  • 1
    Okay, Thanks for keeping SO clean. :D – Faheem Apr 20 '19 at 05:33

0 Answers0