Mori
Mori

Reputation: 2574

How to assign PartitionKey to CosmosDB queries using Node.js SDK v3.5.2

I upgraded CosmosDB Node.js SDK from 2.1.5 to 3.5.2 and the following code is no longer working.

client.items.query(myQuery, { partitionKey: "MyPartitionKey" }).toArray()

I changed the code to the following but it still does not transpile (typescript) and apparently the cause is that FeedOptions no longer contains partitionKey property!

client.items.query(myQuery,{ partitionKey: "MyPartitionKey" }).fetchAll()

I looked it up in the internet but could not find an up to date example.

Any idea how to fix this?

Upvotes: 3

Views: 1102

Answers (2)

Mori
Mori

Reputation: 2574

Turns out in v3 of the sdk there is no need to explicitly assign partitionkey value. If partitionkey engages in where clause, it will automatically be regarded.

githib issue reference

Upvotes: 3

Jay Gong
Jay Gong

Reputation: 23782

I can't find any up-to-date example from MS or github source code which is really challenging.

There is no any partitionkey property in FeedOptions class in the cosmos db node sdk v3 any more.After my deep research on the github source code of FeedOptions,i found that it extends another interface SharedOptions:

enter image description here

And it contains initialHeaders property so that i'm supposed that we could set partition key value following the REST API Header Lists.

enter image description here

Anyway,please refer to my working code:

const feedOptions = {
        initialHeaders: {"x-ms-documentdb-partitionkey": '["a"]'}
      };
   const queryIterator = await container.items.query(querySpec,feedOptions);
    while (queryIterator.hasMoreResults()) {
        console.log(await queryIterator.fetchNext());
    }

Surely,you could replace it with .fetchAll() method.

Upvotes: 1

Related Questions