Hi all,
I’m are currently implementing the extended reference pattern to increase query speed and remove lookups. I hope somebody can give me feedback regarding best practices about working w/ arrays.
My model is something like this (simplified):
user.model.ts
{
_id: ObjectId,
email: string,
name: string
}
tax.model.ts
{
_id: ObjectId,
name: string
rate: number
}
expense.model.ts
{
creator: ObjectId,
reviewer: ObjectId,
splits: [{
name: string,
tax: ObjectId
}]
}
I’m considering the following two approaches:
-
Adding new object
extendedReferences
containing the extended reference dataexpense.model.ts
{ creator: ObjectId, reviewer: ObjectId, splits: [{ name: string, tax: ObjectId }], extendedReferences: { users: [{ _id: ObjectId, email: string, }], taxes: [{ _id: ObjectId, rate: number }], } }
-
Replacing ObjectId w/ the extended reference object (like explained in the blog post)
expense.model.ts
{ creator: { _id: ObjectId, email: string }, reviewer: { _id: ObjectId, email: string }, splits: [{ name: string, tax: { _id: ObjectId, rate: number } }] }
Example for an expense and how I want to store the data using the 1st approach:
{
creator: 1337,
reviewer: 1337,
splits: [
{
name: "1st Split",
tax: 1
},
{
name: "2nd Split",
tax: 2
},
{
name: "3rd Split",
tax: 1
},
],
extendedReferences: {
users: [
{
_id: 1337,
email: "user@gmail.com"
}
],
taxes: [
{
_id: 1,
rate: 10
},
{
_id: 2,
rate: 20
}
]
}
}
I’m considering storing a single extended reference separately because the same tax can be used several times in the splits. Which I hope would give me the following advantages:
- Smaller document size
- Faster writes if extended reference data has to be updated
But I’m wondering if that 2nd point is true or premature optimization.
Furthermore, the downside is application logic for making searches when accessing extended reference data and I’m assuming more complex pipelines when working w/ that data because some find and replace steps are required.
What are the recommended best practices for this use case?