Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertdempsey/457a4aaee40fd671e0968c525d64c404 to your computer and use it in GitHub Desktop.
Save robertdempsey/457a4aaee40fd671e0968c525d64c404 to your computer and use it in GitHub Desktop.
Shows how we can choose to only update documents with older values of our property using timestamps
/** create connection pool to system DB in MongoDB
* assume we are using https://www.npmjs.com/package/mongodb
* assume we begin with:
{
system_id: 123,
a: 1,
b: 2,
c: 3,
last_modified: {
a: '2020-03-10T10:33:13.012Z'
}
}
Receive new state with the following data:
{
system_id: 123,
a: 4,
published: '2020-03-10T10:33:43.012Z'
}
*/
system_states_collection.update(
// find document with timestamp for 'a' less than or equal to our timestamp
{ system_id: 123, 'last.modified.a': { $lte: Date('2020-03-10T10:33:43.012Z') },
{
$set: {
a: 4,
'last_modified.a': Date('2020-03-10T10:33:43.012Z')
},
}
)
/**
* Document will only be updated if it finds a document with a
* value for property 'last_modified.a' less than or equal to our
* timestamp (which is the published time of the update from our source)
*/
/**
Resulting document:
{
system_id: 123,
a: 4,
b: 2,
c: 3,
last_modified: {
a: '2020-03-10T10:33:43.012Z'
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment