私はフィールド "email"と "friends_email"を持つコレクションを持っています。私はMongoDBの使用して、セットアップに次のようなユニークネス制約を希望:MongoDBの2つのフィールドを持つユニークな制約
ませレコードは、電子メールやfriends_emailに対して同じ値を持ちません。これは無効です:
{"email": "[email protected]", "friends_email": "[email protected]" }
2つのレコードはすべてのフィールドで同じ値を持ちます。したがって、次の例では、すべての無効次のようになります。平易な英語で
{ { "email": "[email protected]", "friends_email": "[email protected]" }, { "email": "[email protected]", "friends_email": "[email protected]" } } { { "email": "[email protected]", "friends_email": null }, { "email": "[email protected]", "friends_email": null } } { { "email": null, "friends_email": "[email protected]" }, { "email": null, "friends_email': "[email protected]" } }
、それが何かのようになり、
email
とfriends_email
の連結がnull
とundefined
が空の文字列に合体された状態で、ユニークになります。
このルールをMongoDBで実行するには、どのような方法が最適ですか?
db.collection.ensureIndex(keys、options)はバージョン3.0.0から廃止予定です。> db.collection.ensureIndex()はdb.collection.createIndex()のエイリアスになりました。 –