MongoEngine / MongoEngine/mongoengine
Uniqueness within ListFields
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 4.3k
- Fork
- 1.2k
- Merge medio
- 4h 41m
- PR unite (30g)
- 11
Descrizione
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:
class EmbDoc(EmbeddedDocument):
x = StringField(required=True, unique=True)
class Doc(Document):
l = ListField(EmbeddedDocumentField(EmbDoc))
d = Doc(l=[EmbDoc(x='t'), EmbDoc(x='t')]).save() # No Exception
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:
The unique constraint applies to separate documents in the collection. That is, the unique index prevents separate documents from having the same value for the indexed key, but the index does not prevent a document from having multiple elements or embedded documents in an indexed array from having the same value.
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 .
class EmbDoc(EmbeddedDocument):
x = StringField(required=True, unique=True, doc_unique=True)
class Doc(Document):
l = ListField(EmbeddedDocumentField(EmbDoc))
s = ListField(StringField(), doc_unique=True)
d = Doc(l=[EmbDoc(x='t'), EmbDoc(x='t')]).save() # Now raises an exception.
d = Doc(s=['Conflict', 'Conflict']).save() # Now raises an exception.
Pros:
- Simple to understand.
- No syntax or identifier to parse.
- Contained within the Embedded Document (the parent doesn't need to know the doc_unique field).
Cons:
- Parameter would serve no purpose unless the field is inside of a
ListField, which can lead to user confusion.
Solution Conceptualization 2
Add a doc_unique String parameter to ListField. The Id would be a string containing a '$' wildcard character used to represent an index in the list.
class EmbDoc(EmbeddedDocument):
x = StringField(required=True, unique=True)
class Doc(Document):
l = ListField(EmbeddedDocumentField(EmbDoc), doc_unique='$.x')
s = ListField(StringField(), doc_unique='$')
d = Doc(l=[EmbDoc(x='t'), EmbDoc(x='t')]).save() # Now raises an exception.
d = Doc(s=['Conflict', 'Conflict']).save() # Now raises an exception.
Pros:
- Only need to add a new parameter to a single field type.
- Can only be used when it is allowed to be used.
Cons:
- Parsing an identifier.
- Human readability and user usability.
- Parent must be made aware of any Embedded Document fields that need to be document unique.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la gestione esistente dei campi univoci e il comportamento di ListField/EmbeddedDocument descritto nell’issue, quindi confronta i due design proposti per doc_unique con la limitazione SERVER-1068 di MongoDB. Il lavoro è completato quando viene scelto e implementato un design che rifiuti i valori duplicati all’interno di un documento, preservando al contempo il comportamento attuale di unicità.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- mongodb, python
- Ambito
- databases
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100