|
| 1 | +--- |
| 2 | +title: Create an item in Azure Cosmos DB for NoSQL using JavaScript |
| 3 | +description: Learn how to create an item in your Azure Cosmos DB for NoSQL account using the JavaScript SDK. |
| 4 | +author: seesharprun |
| 5 | +ms.author: sidandrews |
| 6 | +ms.service: cosmos-db |
| 7 | +ms.subservice: nosql |
| 8 | +ms.devlang: javascript |
| 9 | +ms.topic: how-to |
| 10 | +ms.date: 05/17/2023 |
| 11 | +ms.custom: devx-track-js, cosmos-db-dev-journey |
| 12 | +--- |
| 13 | + |
| 14 | +# Create an item in Azure Cosmos DB for NoSQL using JavaScript |
| 15 | + |
| 16 | +[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] |
| 17 | + |
| 18 | +Items in Azure Cosmos DB represent a specific entity stored within a container. In the API for NoSQL, an item consists of JSON-formatted data with a unique identifier. |
| 19 | + |
| 20 | +## Item, item definition, and item response |
| 21 | + |
| 22 | +In the JavaScript SDK, the three objects related to an item have different purposes. |
| 23 | + |
| 24 | +|Name|Operations| |
| 25 | +|--|--| |
| 26 | +|[Item](/javascript/api/@azure/cosmos/item)|Functionality including **Read**, **Patch**, **Replace**, **Delete**.| |
| 27 | +|[ItemDefinition](/javascript/api/@azure/cosmos/itemdefinition)|Your custom data object. Includes `id` and `ttl` properties automatically.| |
| 28 | +|[ItemResponse](/javascript/api/@azure/cosmos/itemresponse)|Includes `statusCode`, `item`, and other properties.| |
| 29 | + |
| 30 | +Use the properties of the **ItemResponse** object to understand the result of the operation. |
| 31 | + |
| 32 | +* **statusCode**: HTTP status code. A successful response is in the 200-299 range. |
| 33 | +* **activityId**: Unique identifier for the operation such as create, read, replace, or delete. |
| 34 | +* **etag**: Entity tag associated with the data. Use for optimistic concurrency, caching, and conditional requests. |
| 35 | +* **item**: [Item](/javascript/api/@azure/cosmos/item) object used to perform operations such as read, replace, delete. |
| 36 | +* **resource**: Your custom data. |
| 37 | + |
| 38 | +## Create a unique identifier for an item |
| 39 | + |
| 40 | +The unique identifier is a distinct string that identifies an item within a container. The ``id`` property is the only required property when creating a new JSON document. For example, this JSON document is a valid item in Azure Cosmos DB: |
| 41 | + |
| 42 | +```json |
| 43 | +{ |
| 44 | + "id": "unique-string-2309509" |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Within the scope of a container, two items can't share the same unique identifier. |
| 49 | + |
| 50 | +> [!IMPORTANT] |
| 51 | +> The ``id`` property is case-sensitive. Properties named ``ID``, ``Id``, ``iD``, and ``_id`` will be treated as an arbitrary JSON property. |
| 52 | +
|
| 53 | +Once created, the URI for an item is in this format: |
| 54 | + |
| 55 | +``https://<cosmos-account-name>.documents.azure.com/dbs/<database-name>/docs/<item-resource-identifier>`` |
| 56 | + |
| 57 | +When referencing the item using a URI, use the system-generated *resource identifier* instead of the ``id`` field. For more information about system-generated item properties in Azure Cosmos DB for NoSQL, see [properties of an item](../resource-model.md#properties-of-an-item) |
| 58 | + |
| 59 | +## Create an item |
| 60 | + |
| 61 | +Create an item with the container's [items](/javascript/api/@azure/cosmos/container#@azure-cosmos-container-items) object using the [create](/javascript/api/@azure/cosmos/items) method. |
| 62 | + |
| 63 | +```javascript |
| 64 | +const { statusCode, item, resource, activityId, etag} = await container.items.create({ |
| 65 | + id: '2', |
| 66 | + category: 'gear-surf-surfboards', |
| 67 | + name: 'Sunnox Surfboard', |
| 68 | + quantity: 8, |
| 69 | + sale: true |
| 70 | + }); |
| 71 | +``` |
| 72 | + |
| 73 | +## Access an item |
| 74 | + |
| 75 | +Access an item through the [Item](/javascript/api/@azure/cosmos/item) object. This can accessed from the [Container](/javascript/api/@azure/cosmos/container) object or changed from either the [Database](/javascript/api/@azure/cosmos/database) or [CosmosClient](/javascript/api/@azure/cosmos/cosmosclient) objects. |
| 76 | + |
| 77 | +```javascript |
| 78 | +// Chained, then use a method of the Item object such as `read` |
| 79 | +const { statusCode, item, resource, activityId, etag} = await client.database(databaseId).container(containerId).item(itemId).read(); |
| 80 | +``` |
| 81 | + |
| 82 | +Access by object: |
| 83 | +* [Items](/javascript/api/@azure/cosmos/items) (plural): Create, batch, watch change feed, read all, upsert, or query items. |
| 84 | +* [Item](/javascript/api/@azure/cosmos/item) (singular): Read, patch, replace, or delete an item. |
| 85 | + |
| 86 | +## Replace an item |
| 87 | + |
| 88 | +Replace the data with the [Item](/javascript/api/@azure/cosmos/item) object with the [replace](/javascript/api/@azure/cosmos/item#@azure-cosmos-item-replace) method. |
| 89 | + |
| 90 | +```javascript |
| 91 | +const { statusCode, item, resource, activityId, etag} = await item.replace({ |
| 92 | + id: '2', |
| 93 | + category: 'gear-surf-surfboards-retro', |
| 94 | + name: 'Sunnox Surfboard Retro', |
| 95 | + quantity: 5, |
| 96 | + sale: false |
| 97 | + }); |
| 98 | +``` |
| 99 | + |
| 100 | +## Read an item |
| 101 | + |
| 102 | +Read the most current data with the [Item](/javascript/api/@azure/cosmos/item) object's [read](/javascript/api/@azure/cosmos/item#@azure-cosmos-item-read) method. |
| 103 | + |
| 104 | +```javascript |
| 105 | +const { statusCode, item, resource, activityId, etag} = await item.read(); |
| 106 | +``` |
| 107 | + |
| 108 | +## Delete an item |
| 109 | + |
| 110 | +Delete the item with the [Item](/javascript/api/@azure/cosmos/item) object's' [delete](/javascript/api/@azure/cosmos/item#@azure-cosmos-item-read) method. |
| 111 | + |
| 112 | +```javascript |
| 113 | +const { statusCode, item, activityId, etag} = await item.delete(); |
| 114 | +``` |
| 115 | + |
| 116 | +## Next steps |
| 117 | + |
| 118 | +Now that you've created various items, use the next guide to query for item. |
| 119 | + |
| 120 | +> [!div class="nextstepaction"] |
| 121 | +> [Read an item](how-to-javascript-query-items.md) |
0 commit comments