Open bathos opened 5 years ago
That's correct. You're looking for a plugin that ensures elements in an array are unique within a document, but not within a collection? Can you please describe a use case for that?
The constraint corresponds to any array that’s intended to be used as a set (whether treated as ordered or unordered). Say an array of enum strings or references — blogPost.tags is a ubiquitous example.
I went looking for a plugin because I find myself writing roughly the same validator for array member uniqueness frequently. In my experience at least, the general case (array is a set) is more common than the more specific case this plugin covers (array is a set + members are unique across all documents). That case has come up for me too, of course, I was just hoping this could be used for both.
Mongoose (and mongo proper) provide methods for use when arrays are intended to behave as sets (addToSet, pull). They just don’t come with any built-in validation that lets you enforce that the array remains a set (afaik).
(With my question answered — thanks — feel free to close this issue, unless you want to consider it as a feature request.)
I'll leave this open as a feature request.
That'd be very helpful actually, it's exactly what I'm looking for
If I understand right, this plugin:
is that accurate? I was hoping I could use this for both cases where there is and is not a collection-wide uniqueness constraint and it seems like only the former is supported.