Open mmellison opened 9 years ago
Any feedback or comments are appreciated. In many of my past projects, I just added a validation method to check for duplicates but IMO it would make things a lot cleaner if it was handled internally by MongoEngine.
Without being able to choose a better solution, I think this feature would be great. I'm also doing the same with own validate method, it's a pity mongo doesn't support it directly, but it would be great to have this out-of-the-box with mongoengine
Hopefully we see some support in MongoDB for this problem soon. I see https://jira.mongodb.org/browse/SERVER-1068 is now marked as in "Planning Bucket A". But who knows what that means :)
This means actually nothing... it won't come out before ages...
I would else vote for Concept1: less cons :-p
Yes -- it could take years before we see that feature implemented in MongoDB, but such is the nature of OSS. I will be revisiting this feature in the near future. There are some larger changes I want to propose first that would make feature like that a bit easier to implement.
Is something going on for this?
Any feedback or comments are appreciated. In many of my past projects, I just added a validation method to check for duplicates but IMO it would make things a lot cleaner if it was handled internally by MongoEngine.
Does anything exist for forcing uniqueness within EmbeddedDocumentListField
or I have to manually check it?
Edit : add_to_set
does exactly what I needed. Thanks!
Feature Request
Description
Uniqueness within ListFields would allow MongoEngine to assert uniqueness of lists containing fields and embedded documents, within the same document.
Background
Currently MongoDB does not enforce unique indices within the same document. For example:
The saved document will contain duplicate entries for
x
, despite it being marked as unique. This is to be expected and is the normal behavior of MongoDB as of today.Looking at http://docs.mongodb.org/manual/core/index-unique/ we can see the following:
More information about the issue can be found at http://joegornick.com/2012/10/25/mongodb-unique-indexes-on-single-embedded-documents/
MongoDB has had an open issue to add support for this since 2010. https://jira.mongodb.org/browse/SERVER-1068
Proposed Solution
I propose that until MongoDB addresses the above linked issue, if they ever do, uniqueness with ListFields is asserted via MongoEngine implementation.
If MongoDB is to ever address the issue, the internal implementation for MongoEngine can be switched to use Unique 'Doc' Indices in the same manner that unique fields are handled today via unique collection indices.
Solution Conceptualization 1
Add a
doc_unique
Boolean parameter to Fields .Pros:
Cons:
ListField
, which can lead to user confusion.Solution Conceptualization 2
Add a
doc_unique
String parameter toListField
. The Id would be a string containing a '$' wildcard character used to represent an index in the list.Pros:
Cons: