Skip to content

Commit 0a56381

Browse files
committed
fix samples
1 parent 358af4c commit 0a56381

File tree

2 files changed

+76
-24
lines changed

2 files changed

+76
-24
lines changed

articles/cosmos-db/hierarchical-partition-keys.md

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ Container container = await database.CreateContainerIfNotExistsAsync(containerPr
134134

135135
#### [Java SDK v4](#tab/java-v4)
136136

137-
```java
137+
#### [JavaScript SDK v4](#tab/javascript-v4)
138+
139+
```javascript
138140
// List of partition keys, in hierarchical order. You can have up to three levels of keys.
139141
List<String> subpartitionKeyPaths = new ArrayList<String>();
140142
subpartitionKeyPaths.add("/TenantId");
@@ -250,22 +252,26 @@ UserSession item = new UserSession()
250252
ItemResponse<UserSession> createResponse = await container.CreateItemAsync(item);
251253
```
252254

253-
##### [Java SDK v4](#tab/java-v4)
255+
##### [Javascript SDK v4](#tab/javascript-v4)
256+
257+
```javascript
258+
// Create a new item
259+
const item: UserSession = {
260+
Id: 'f7da01b0-090b-41d2-8416-dacae09fbb4a',
261+
TenantId: 'Microsoft',
262+
UserId: '8411f20f-be3e-416a-a3e7-dcd5a3c1f28b',
263+
SessionId: '0000-11-0000-1111'
264+
}
254265

255-
```java
256-
// Create a new item
257-
UserSession item = new UserSession();
258-
item.setId("f7da01b0-090b-41d2-8416-dacae09fbb4a");
259-
item.setTenantId("Microsoft");
260-
item.setUserId("8411f20f-be3e-416a-a3e7-dcd5a3c1f28b");
261-
item.setSessionId("0000-11-0000-1111");
262-
263266
// Pass in the object, and the SDK automatically extracts the full partition key path
264-
Mono<CosmosItemResponse<UserSession>> createResponse = container.createItem(item);
267+
const { resource: document } = await = container.items.create(item);
268+
265269
```
266270

267271
---
268272

273+
// Pass in the object, and the SDK automatically extracts the full partition key path
274+
const { resource: document } = await = container.items.create(it
269275
#### Manually specify the path
270276

271277
The `PartitionKeyBuilder` class in the SDK can construct a value for a previously defined hierarchical partition key path. Use this class when you add a new item to a container that has subpartitioning enabled.
@@ -319,6 +325,28 @@ Mono<CosmosItemResponse<UserSession>> createResponse = container.createItem(item
319325

320326
---
321327

328+
##### [Javascript SDK v4](#tab/javascript-v4)
329+
330+
```javascript
331+
const item: UserSession = {
332+
Id: 'f7da01b0-090b-41d2-8416-dacae09fbb4a',
333+
TenantId: 'Microsoft',
334+
UserId: '8411f20f-be3e-416a-a3e7-dcd5a3c1f28b',
335+
SessionId: '0000-11-0000-1111'
336+
}
337+
338+
// Specify the full partition key path when creating the item
339+
const partitionKey: PartitionKey = new PartitionKeyBuilder()
340+
.addValue(item.TenantId)
341+
.addValue(item.UserId)
342+
.addValue(item.SessionId)
343+
.build();
344+
345+
// Create the item in the container
346+
const { resource: document } = await container.items.create(item, partitionKey);
347+
348+
---
349+
322350
### Perform a key/value lookup (point read) of an item
323351

324352
Key/value lookups (point reads) are performed in a way that's similar to a non-subpartitioned container. For example, assume you have a hierarchical partition key that consists of `TenantId` > `UserId` > `SessionId`. The unique identifier for the item is a GUID. It's represented as a string that serves as a unique document transaction identifier. To perform a point read on a single item, pass in the `id` property of the item and the full value for the partition key, including all three components of the path.
@@ -359,26 +387,22 @@ PartitionKey partitionKey = new PartitionKeyBuilder()
359387
// Perform a point read
360388
Mono<CosmosItemResponse<UserSession>> readResponse = container.readItem(id, partitionKey, UserSession.class);
361389
```
362-
363-
---
364-
365-
##### [JavaScript SDK v4](#tab/javascript-v4)
390+
##### [Javascript SDK v4](#tab/javascript-v4)
366391

367392
```javascript
368393
// Store the unique identifier
369-
String id = "f7da01b0-090b-41d2-8416-dacae09fbb4a";
394+
const id = "f7da01b0-090b-41d2-8416-dacae09fbb4a";
370395
371396
// Build the full partition key path
372-
PartitionKey partitionKey = new PartitionKeyBuilder()
373-
.add("Microsoft") //TenantId
374-
.add("8411f20f-be3e-416a-a3e7-dcd5a3c1f28b") //UserId
375-
.add("0000-11-0000-1111") //SessionId
397+
const partitionKey: PartitionKey = new PartitionKeyBuilder()
398+
.addValue(item.TenantId)
399+
.addValue(item.UserId)
400+
.addValue(item.SessionId)
376401
.build();
377-
402+
378403
// Perform a point read
379-
Mono<CosmosItemResponse<UserSession>> readResponse = container.readItem(id, partitionKey, UserSession.class);
404+
const { resource: document } = await container.item(id, partitionKey).read();
380405
```
381-
382406
---
383407

384408
### Run a query
@@ -446,6 +470,20 @@ pagedResponse.byPage().flatMap(fluxResponse -> {
446470
return Flux.empty();
447471
}).blockLast();
448472
```
473+
##### [Javascript SDK v4](#tab/javascript-v4)
474+
475+
```javascript
476+
// Define a single-partition query that specifies the full partition key path
477+
const query: string = "SELECT * FROM c WHERE c.TenantId = 'Microsoft' AND c.UserId = '8411f20f-be3e-416a-a3e7-dcd5a3c1f28b' AND c.SessionId = '0000-11-0000-1111'";
478+
479+
// Retrieve an iterator for the result set
480+
const queryIterator = container.items.query(query);
481+
482+
while (queryIterator.hasMoreResults()) {
483+
const { resources: results } = await queryIterator.fetchNext();
484+
// Process result
485+
}
486+
```
449487
450488
---
451489
@@ -495,6 +533,20 @@ pagedResponse.byPage().flatMap(fluxResponse -> {
495533
}).blockLast();
496534
```
497535

536+
##### [Javascript SDK v4](#tab/javascript-v4)
537+
538+
```javascript
539+
// Define a targeted cross-partition query specifying prefix path[s]
540+
const query: string = "SELECT * FROM c WHERE c.TenantId = 'Microsoft'";
541+
542+
// Retrieve an iterator for the result set
543+
const queryIterator = container.items.query(query);
544+
545+
while (queryIterator.hasMoreResults()) {
546+
const { resources: results } = await queryIterator.fetchNext();
547+
// Process result
548+
}
549+
```
498550
---
499551

500552
## Limitations and known issues

articles/cosmos-db/nosql/change-feed-pull-model.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ Here's an example of how to obtain the iterator in latest version mode that retu
310310

311311
```js
312312
const options = {
313-
changeFeedStartFrom: ChangeFeedStartFrom.Beginning()
313+
changeFeedStartFrom: ChangeFeedStartFrom.Now()
314314
};
315315

316316
const iterator = container.items.getChangeFeedIterator(options);

0 commit comments

Comments
 (0)