Skip to content

Commit 6b87495

Browse files
committed
Refine cosmos input v2 samples to match v1 more.
1 parent 967d360 commit 6b87495

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

articles/azure-functions/functions-bindings-cosmosdb-v2-input.md

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,16 +1531,29 @@ The following example shows an Azure Cosmos DB input binding. The function reads
15311531
# [v2](#tab/python-v2)
15321532

15331533
```python
1534-
@app.route()
1534+
@app.queue_trigger(arg_name="msg",
1535+
queue_name="outqueue",
1536+
connection="AzureWebJobsStorage")
15351537
@app.cosmos_db_input(
1536-
arg_name="documents", database_name="<DB_NAME>",
1537-
collection_name="<COLLECTION_NAME>",
1538-
connection_string_setting="CONNECTION_STRING")
1539-
def test_function(req: func.HttpRequest, id: func.In[func.QueueMessage],
1540-
documents: func.DocumentList) -> func.HttpResponse:
1538+
arg_name="documents",
1539+
database_name="MyDatabase",
1540+
collection_name="MyCollection",
1541+
id="{msg.payload_property}",
1542+
partition_key="{msg.payload_property}",
1543+
connection_string_setting="MyAccount_COSMOSDB")
1544+
@app.cosmos_db_output(arg_name="outputDocument",
1545+
database_name="MyDatabase",",
1546+
collection_name="MyCollection",
1547+
connection_string_setting="MyAccount_COSMOSDB")
1548+
def test_function(msg: func.QueueMessage,
1549+
inputDocument: func.DocumentList,
1550+
outputDocument: func.Out[func.Document]):
15411551
document = documents[id]
15421552
document["text"] = "This was updated!"
1543-
return document
1553+
doc = inputDocument[0]
1554+
doc["text"] = "This was updated!"
1555+
outputDocument.set(doc)
1556+
print(f"Updated document.")
15441557
```
15451558

15461559
# [v1](#tab/python-v1)
@@ -1597,15 +1610,16 @@ The following example shows a function that retrieves a single document. The fun
15971610
```python
15981611
@app.route()
15991612
@app.cosmos_db_input(
1600-
arg_name="documents", database_name="<DB_NAME>",
1601-
collection_name="<COLLECTION_NAME>",
1602-
connection_string_setting="CONNECTION_STRING")
1613+
arg_name="documents",
1614+
database_name="ToDoItems",
1615+
collection_name="Items",
1616+
connection_string_setting="CosmosDBConnection")
16031617
def test_function(req: func.HttpRequest,
16041618
todoitems: func.DocumentList) -> func.HttpResponse:
16051619
id = req.params.get('id')
16061620
partitionKeyValue = req.params.get('partitionKeyValue')
16071621
if not todoitems:
1608-
logging.warning("ToDo items not found")
1622+
logging.warning("ToDoItems not found")
16091623
else:
16101624
logging.info("Found ToDo item, Text=%s",
16111625
todoitems[0]['description'])
@@ -1679,10 +1693,11 @@ The following example shows a function that retrieves a single document. The fun
16791693

16801694
```python
16811695
@app.function_name()
1682-
@app.route(route="todoitems/{partitionKey}/{id}", auth_level=func.AuthLevel.ANONYMOUS)
1696+
@app.route(route="todoitems/{partitionKey}/{id}")
16831697
@app.cosmos_db_input(
1684-
arg_name="documents", database_name="<DB_NAME>",
1685-
collection_name="<COLLECTION_NAME>",
1698+
arg_name="todoitems",
1699+
database_name="ToDoItems",
1700+
collection_name="Items",
16861701
connection_string_setting="CONNECTION_STRING",
16871702
partition_key="{partitionKey}",
16881703
id="{id}")
@@ -1695,9 +1710,6 @@ def test_function(req: func.HttpRequest,
16951710
todoitems[0]['description'])
16961711

16971712
return 'OK'
1698-
1699-
return 'OK'
1700-
17011713
```
17021714

17031715
# [v1](#tab/python-v1)

0 commit comments

Comments
 (0)