PeterJoe
PeterJoe

Reputation: 113

Mongoose Children

im still new in mongoose i would like to get some advice.

I have a model like this

    const entrySchema - new mongoose.Schema({
    companyID: {type: mongoose.Schema.ObjectId},
    ...etc
    entries: Array
    })

So i would like to add entries in the array. It would look something like this

entries: [{name: '1', text: '', smaple: ''}, {name: '2', text: '', smaple: ''}]

so if i want to edit entry.entries[0] that would be the first entry, is it save to use the index as "id"

Or do i have to create a separate model called Entries with a entryID and then populate them that why i would have a unique id for each one.

Hope it makes sense. What would be the best solution?

Upvotes: 1

Views: 35

Answers (1)

Nafeo Alam
Nafeo Alam

Reputation: 4692

  1. Well, just think, if you have 1000 entries then it will go back and forth 1000 times between the collections which will take a lot of time. Whereas, if you keep entries in the same collection it is going to be much optimum performance-wise.

  2. But, on the other hand, let's say you need all the transactions to find out the daily update report. Then it will be better to have a separate collection for all the entries.

  3. There is another solution where you could keep both "Array with necessary information" and "separate collection with all the information", thus you will be able to do both when it is necessary. Although for this, you will need to write an extra bit of code and maintain all the transactions.

Now, it's your decision.

Upvotes: 1

Related Questions