Reputation: 697
I have a Video Document, each video has some certain segments it's might be from 100 to 5000 based on video duration.
videoModel = {
id: 'vid334',
codec: 'avc1.4d401e,mp4a.40.2',
resolution : { "width": 854, "height": 480 },
segments : [
{
segment : 1,
file : seg-1.ts
},
{
segment : 1,
file : seg-1.ts
},
....
]
}
once each video created there is no need to update video or segments.
now I want to know what is the best way to save segments? should I embed segments in a video document or save each segment in a separate collection?
Upvotes: 2
Views: 353
Reputation: 71120
There is no best way; only you can decide that. There is no rule about embedding vs referencing, and it is common to reference documents from other documents. But objectively, consider this:
The issue you'll eventually run into is the notion of "unbounded arrays" - a major code-smell with embedded arrays of documents. At some point, with enough segments, and an unlimited number of segments. you will hit the size limit of a document. At that point, your app will effectively be broken, as you won't be able to store additional segments. And you'd need to refactor your code (and model) to deal with that situation.
If your number of segments is bounded, and you are sure you won't ever exceed the size of a single document, then the unbounded-array condition (and associated risk) does not apply.
One other issue is with replication and performance, when using such a large array. The entire array is included in the oplog for every addition to the array ($push()
), since the oplog is idempotent (you can replay entries multiple times, with the same results). So as your array grows, so does the size of the entry in the oplog, and replication may slow down and impact your overall performance.
Upvotes: 4