Summary
I have a collection where I have a combination of attributes forming a unique constraint on my collection.
An example is a client raising an order. The client use case is that if a user wants to create two orders on the same day, they have to edit the first order instead of generating a new one.
What is the best way to enforce this? Strategies I’ve thought through of so far are:
-
Define ObjectID: The string passed to the ObjectID could be generated as the result of the values of the key columns. before any call to
insert
orupsert
.
-
Create Additional Attribute: You can apply the SimpleSchema restriction
unique: true
fromaldeed:schema-index
to a compound attribute. -
Extend the
Mongo.Collection
: Extend the default collection and then override theinsert
andupsert
methods. -
Roll it in Mongo: The unique restraint can be defined on a compound index natively in MongoDB and I believe can be accessed through
collection.rawDatabase
I might have missed an option here, but I really dislike all of these options for various reasons. I’ve so far applied the third, but consider to move and wonder how other people address this issue.
1. Define ObjectID
I don’t like this because: The overhead for creating my own ObjectID feels wasteful and unless also applying step 3 and overriding the insert/upsert methods, it will be difficult to enforce this throughout a project with multiple developers.
2. Create Additional Attribute
To create an additional attribute again feels wasteful and also needs to be enforced. The compound keys are not immutable and may be changed for any document, so they may need to be updated - requiring me to also catch any updates.
3. Extend the Mongo Collection
Whenever I have overriden the insert
, upsert
and update
methods for all my collections to check first if the object exists. I currently don’t handle updates this way as the up until now the attributes making up the key were immutable.
4. Roll it in Mongo
This is currently my preferred approach. The biggest downside I see is that a whole bunch of errors need to be handled when updates fail as a result of index changes.
I also wondered if someone has effectively done this in the form of a package, but could not find any.