Reference

Endpoint Create a document in the container


Name

create_document

Description

Insert JSON document in Cosmos DB Container. [API reference]

Related Tables

[Dynamic Table]

Parameters

Parameter Required Options
Name: Table

Label: Table Name (Case-Sensitive)

YES
Name: Database

Label: Database Name (keep blank to use default) Case-Sensitive

Leave blank to use default DB set on connection screen
Name: Document

Label: Document

Name: PartitionKey

Label: Partition Key Value (default is supplied Id)

The partition key value for the document. Must be included if and only if the collection is created with a partitionKey definition
Option Value
Default .
SingleKeyValue ["someValue1"]
MultiKeyValue ["some_value1","some_value2" ]
Name: Upsert

Label: Enable Upsert Mode (update if document found)

Option Value
true true
false false

Output Columns

Label Data Type (SSIS) Data Type (SQL) Length Description
id DT_WSTR nvarchar(4000) 4000
_rid DT_WSTR nvarchar(4000) 4000
_ts DT_I8 bigint
http_status DT_I4 int
If the column you are looking for is missing, consider customizing Cosmos DB Connector.

Input Columns

Label Data Type (SSIS) Data Type (SQL) Length Description
PartitionKey DT_WSTR nvarchar(4000) 4000
Document DT_NTEXT nvarchar(MAX)
Required columns that you need to supply are bolded.

Examples

SSIS

Use Cosmos DB Connector in API Source or in API Destination SSIS Data Flow components to read or write data.

API Source

API Source - Cosmos DB
Read and write Azure Cosmos DB data effortlessly. Query, integrate, and manage databases, containers, documents, and users — almost no coding required.
Cosmos DB
Create a document in the container
There are no parameters to configure.
SSIS API Source - Read from table or endpoint

API Destination

This Endpoint belongs to the [Dynamic Table] table, therefore it is better to use it, instead of accessing the endpoint directly. Use this table and table-operation pair to create a document in the container:

API Destination - Cosmos DB
Read and write Azure Cosmos DB data effortlessly. Query, integrate, and manage databases, containers, documents, and users — almost no coding required.
Cosmos DB
[Dynamic Table]
Insert
There are no parameters to configure.
SSIS API Destination - Access table operation

ODBC application

Use these SQL queries in your ODBC application data source:

Create document with partition key

<p>Inserts a new document into the container. If the container has a partition key, supply it in the <code>PartitionKey</code> column as a JSON array (e.g. ["user2"]). The value must match the document attribute used as the partition key. The <code>Document</code> column holds the full JSON body. Use the <code>Upsert</code> option in <code>WITH</code> to update if a document with the same <code>id</code> already exists.</p>

INSERT INTO TestContainer (PartitionKey, Document)
VALUES(
'["user2"]', --partition key value must match its attribute from document else it will throw error. In this example container PartitionKey is /id so we used its value. For multiple key use JSON array ["val1","val2"]
'{
  "id": "user2",
  "name": "John Doe",
  "email": "jdoe@contoso.com",
  "phone": ["12345"],
  "level": "platinum"
}'
)
WITH(Upsert='true')

Create document from file path

<p>Inserts a document whose JSON body is read from a local file. The file path must start with the <code>@</code> symbol (e.g. @c:\data\order.json). Set <code>IsMultiPart=1</code> in <code>WITH</code> so the driver reads from disk. Partition key rules are the same as for inline document insert.</p>

INSERT INTO TestContainer (PartitionKey, Document)
VALUES(
'["user2"]', --partition key value must match its attribute from document else it will throw error. In this example container PartitionKey is /id so we used its value. For multiple key use JSON array ["val1","val2"]
'@c:\data\order.json'   --path must start with @ symbol
)
WITH(Upsert='true', IsMultiPart=1)

Upsert document (insert or update if exists)

<p>Inserts the document or updates it if a document with the same <code>id</code> already exists. Use the <code>Upsert</code> option in <code>WITH</code> on <code>INSERT</code> to enable upsert. Handy for sync or idempotent loads where you do not want to fail on duplicate id.</p>

INSERT INTO TestContainer (PartitionKey, Document)
VALUES(
'["user2"]', --partition key value must match its attribute from document else it will throw error. In this example container PartitionKey is /id so we used its value. For multiple key use JSON array ["val1","val2"]
'{
  "id": "user2",
  "name": "John Doe",
  "email": "jdoe@contoso.com",
  "phone": ["12345"],
  "level": "platinum"
}')
 WITH(Upsert='true')

create_document endpoint belongs to [Dynamic Table] table(s), and can therefore be used via those table(s).

SQL Server

Use these SQL queries in SQL Server after you create a data source in Data Gateway:

Create document with partition key

<p>Inserts a new document into the container. If the container has a partition key, supply it in the <code>PartitionKey</code> column as a JSON array (e.g. ["user2"]). The value must match the document attribute used as the partition key. The <code>Document</code> column holds the full JSON body. Use the <code>Upsert</code> option in <code>WITH</code> to update if a document with the same <code>id</code> already exists.</p>

DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO TestContainer (PartitionKey, Document)
VALUES(
''["user2"]'', --partition key value must match its attribute from document else it will throw error. In this example container PartitionKey is /id so we used its value. For multiple key use JSON array ["val1","val2"]
''{
  "id": "user2",
  "name": "John Doe",
  "email": "jdoe@contoso.com",
  "phone": ["12345"],
  "level": "platinum"
}''
)
WITH(Upsert=''true'')';

EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];

Create document from file path

<p>Inserts a document whose JSON body is read from a local file. The file path must start with the <code>@</code> symbol (e.g. @c:\data\order.json). Set <code>IsMultiPart=1</code> in <code>WITH</code> so the driver reads from disk. Partition key rules are the same as for inline document insert.</p>

DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO TestContainer (PartitionKey, Document)
VALUES(
''["user2"]'', --partition key value must match its attribute from document else it will throw error. In this example container PartitionKey is /id so we used its value. For multiple key use JSON array ["val1","val2"]
''@c:\data\order.json''   --path must start with @ symbol
)
WITH(Upsert=''true'', IsMultiPart=1)';

EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];

Upsert document (insert or update if exists)

<p>Inserts the document or updates it if a document with the same <code>id</code> already exists. Use the <code>Upsert</code> option in <code>WITH</code> on <code>INSERT</code> to enable upsert. Handy for sync or idempotent loads where you do not want to fail on duplicate id.</p>

DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO TestContainer (PartitionKey, Document)
VALUES(
''["user2"]'', --partition key value must match its attribute from document else it will throw error. In this example container PartitionKey is /id so we used its value. For multiple key use JSON array ["val1","val2"]
''{
  "id": "user2",
  "name": "John Doe",
  "email": "jdoe@contoso.com",
  "phone": ["12345"],
  "level": "platinum"
}'')
 WITH(Upsert=''true'')';

EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];

create_document endpoint belongs to [Dynamic Table] table(s), and can therefore be used via those table(s).