CosmosDB C# sql not retunring results

I'm sure I've missed something obvious. The items list is always empty

.Net Core 2.1 web app

Microsoft.Azure.DocumentDB.Core 2.2.1

                var opts = new FeedOptions()
            {
                PartitionKey = new PartitionKey("/clientId"),  
                EnableCrossPartitionQuery = true,
                EnableScanInQuery = true,                       
            };
            string sql = "SELECT * FROM c "; 
            var items = client.CreateDocumentQuery(collectionLink: collectionLink.ToString(), sqlExpression: sql, feedOptions: opts).ToList();

            if (!items.Any())
            {

Using the where clause in the portal, it worked as expected. In the portal, I've verified that the collection has documents (5 currently). The code without exception, so my database/collection names are correct, as is the collectionLink.

So I tried a different collection with 1000s of documents I have in the same DB -- same thing, no results returned.

                var sql = "SELECT top 10 * FROM c";
            IQueryable<dynamic> query = client.CreateDocumentQuery<string>(collectionLink, new SqlQuerySpec(sql), opts);

            foreach (string alias in query)
            {
                Console.WriteLine(alias);
            }

What am I missing?

Upvotes: 0

Views: 468

Answers (1)

Jay Gong
Jay Gong

Reputation: 23782

Per my experience, you maybe set the incorrect value of partition key here.

PartitionKey = new PartitionKey("/clientId")

The partition key need to be set as the field value, not the field name.

For example, my documents as below :

{
    "id": "1",
    "name": "jay"
}

{
    "id": "2",
    "name": "jay2"
}

My partitionkey is 'name', so here I have two paritions : 'jay' and 'jay1'.

You should set the partitionkey property to 'jay' or 'jay2',not 'name'.

Upvotes: 2

Related Questions