Goal
- Zero Conflict System: Having this be a write-only system would save us from conflicts. People are creating and updating documents both offline and online and being able to figure out what update trumps what is important.
- Deep Historical reference: I want to know at any period of time, what that document looked like. On top of that, I need a deep historical analysis of how each item changes over time.
I was thinking of the following architecture:
Reference Document
_id: "u12345",
type: "user",
createdAt: 1584450565 //UNIX TIMESTAMP
{
_id: "<random>"
type: "user-name-revision" //{type}-{key}-Revision
referenceId: "u12345"
value: "John Doe Boy"
updatedAt: 1584450565
}
{
_id: "<random>"
type: "user-name-revision"
referenceId: "u12345"
value: "John Doe"
updatedAt: 1584450566 // 1 second higher than the above
}
{
_id: "<random>"
type: "user-email-revision"
referenceId: "u12345"
value: "john@gmail.com"
updatedAt: 1584450565
}
If you want to get the user, you would:
- Get all documents with
referenceId
of u12345. - Only get the most recent of each type
- Then combine and output the user like so:
_id: "u12345",
type: "user",
createdAt: 1584450565,
name: "John Doe"
email: "john@gmail.com"
updatedAt: 1584450566 // highest timestamp
The only issue I see is if I wanted to sort all users by name
let's say - If I have 1000 users, I don't see a clean way of doing this.
I was wondering if anyone had any suggestions for a pattern I could use. I'm using MongoDB so I have the power of that at my disposal.