Azure Cosmos DB コンテナーの一意のキーを定義する

適用対象: NoSQL

この記事では、Azure Cosmos DB コンテナーを作成するときに、一意のキーを定義するさまざまな方法を示します。 現在、Azure portal を使用するか、Sdk のいずれかでこの操作を実行することになります。

Azure ポータルの使用

  1. Azure portal にサインインします。

  2. 新しい Azure Cosmos DB アカウントを作成するか、既存のものを選択します。

  3. [データ エクスプローラー] ウィンドウを開いて、操作の対象となるコンテナーを選択します。

  4. [新しいコンテナー] をクリックします。

  5. [コンテナーの追加] ダイアログで、 [+ Add unique key] (+ 一意のキーの追加) をクリックして一意のキー エントリを追加します。

  6. 一意キー制約のパスを入力します

  7. 必要な場合は、 [+ Add unique key] (+ 一意のキーの追加) をクリックして、一意のキー エントリを追加します

    Azure portal での一意キー制約エントリのスクリーンショット

PowerShell の使用

一意のキーを持つコンテナーを作成する方法については、一意のキーと TTL を使用した Azure Cosmos DB コンテナーの作成に関するページを参照してください

.NET SDK を使用する

.NET SDK v2 を使用して新しいコンテナーを作成するときに、UniqueKeyPolicy オブジェクトを使用して一意キー制約を定義できます。

client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri("database"), new DocumentCollection
{
    Id = "container",
    PartitionKey = new PartitionKeyDefinition { Paths = new Collection<string>(new List<string> { "/myPartitionKey" }) },
    UniqueKeyPolicy = new UniqueKeyPolicy
    {
        UniqueKeys = new Collection<UniqueKey>(new List<UniqueKey>
        {
            new UniqueKey { Paths = new Collection<string>(new List<string> { "/firstName", "/lastName", "/emailAddress" }) },
            new UniqueKey { Paths = new Collection<string>(new List<string> { "/address/zipCode" }) }
        })
    }
});

Java SDK の使用

Java SDK を使用して新しいコンテナーを作成するときに、UniqueKeyPolicy オブジェクトを使用して一意キー制約を定義できます。

// create a new DocumentCollection object
DocumentCollection container = new DocumentCollection();
container.setId("container");

// create array of strings and populate them with the unique key paths
Collection<String> uniqueKey1Paths = new ArrayList<String>();
uniqueKey1Paths.add("/firstName");
uniqueKey1Paths.add("/lastName");
uniqueKey1Paths.add("/emailAddress");
Collection<String> uniqueKey2Paths = new ArrayList<String>();
uniqueKey2Paths.add("/address/zipCode");

// create UniqueKey objects and set their paths
UniqueKey uniqueKey1 = new UniqueKey();
UniqueKey uniqueKey2 = new UniqueKey();
uniqueKey1.setPaths(uniqueKey1Paths);
uniqueKey2.setPaths(uniqueKey2Paths);

// create a new UniqueKeyPolicy object and set its unique keys
UniqueKeyPolicy uniqueKeyPolicy = new UniqueKeyPolicy();
Collection<UniqueKey> uniqueKeys = new ArrayList<UniqueKey>();
uniqueKeys.add(uniqueKey1);
uniqueKeys.add(uniqueKey2);
uniqueKeyPolicy.setUniqueKeys(uniqueKeys);

// set the unique key policy
container.setUniqueKeyPolicy(uniqueKeyPolicy);

// create the container
client.createCollection(String.format("/dbs/%s", "database"), container, null);

Node.js SDK の使用

Node.js SDK を使用して新しいコンテナーを作成するときに、UniqueKeyPolicy オブジェクトを使用して一意キー制約を定義できます。

client.database('database').containers.create({
    id: 'container',
    uniqueKeyPolicy: {
        uniqueKeys: [
            { paths: ['/firstName', '/lastName', '/emailAddress'] },
            { paths: ['/address/zipCode'] }
        ]
    }
});

Python SDK の使用

Python SDK を使用して新しいコンテナーを作成するときに、パラメーターとして渡されるディクショナリの一部として一意キー制約を指定できます。

client.CreateContainer('dbs/' + config['DATABASE'], {
    'id': 'container',
    'uniqueKeyPolicy': {
        'uniqueKeys': [
            {'paths': ['/firstName', '/lastName', '/emailAddress']},
            {'paths': ['/address/zipCode']}
        ]
    }
})

次のステップ