MongoEngine / MongoEngine/mongoengine
Uniqueness within ListFields
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 4.3k
- フォーク
- 1.2k
- 平均マージ
- 4時間 41分
- マージ済み PR(30日)
- 11
説明
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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
既存の一意フィールド処理と、issue に記載されている ListField/EmbeddedDocument の動作から始め、次に提案されている 2 つの doc_unique の設計を MongoDB の SERVER-1068 の制限と比較します。完了条件は、ドキュメント内の重複値を拒否しつつ、現在の一意性の動作を維持する設計を選択して実装することです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- mongodb, python
- 領域
- databases
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 25/100