I have an array of objects that look like this:
{ type: 'foo', value : 2, hashedIdentifier : ASDZXC, createdAt : '2022-02-27T14:17:44.860+00:00' }
I would like to insert them in bulk into a MongoDB collection.
My business logic demands that no 2 identical objects are created within a 7 day period. For that I use the hashedIdentifier field to find duplicates, where createdAt is $gte "7 days ago", filter duplicates out of the array, and then insert what remains in the array.
These are 2 different DB queries, and I'm looking for something a little more hermetic and atomic.
I was looking at updateMany with upsert, but couldn't figure this out.
Is there a way to insert all the array objects in bulk, only inserting those objects who do not conflict with the above constraint (identifier + createdAt range)?
- A unique (or compound unique) index is no use for me here, as I do want to enable the creation of duplicates after the time range (7 days) has passed.
- a TTL on collection objects is also out of the question, as I want to keep them all, forever.