简体   繁体   中英

Cosmos DB fan out query issue

I recently started moving our CosmosDB single partitioned collection to new logical partitioned collection. I am doing this in Phases, where I have already covered the all the create and update operation with the support of the transaction. Now currently, I am going to fan out a query for all read operations and I will fix this fan out in few coming weeks. However, In my current scenario, I am having one issue in the query. Below is the code sample of my select using C# LINQ and then I will explain u my issue in few steps

Code without Partition key

using (var query = _client.CreateDocumentQuery<User>(_documentCollectionUri,
                    new FeedOptions { EnableCrossPartitionQuery = true })
                                          .Where(u => u.Email == emailAddress.ToLower())
                                          .AsDocumentQuery())
                {
                    if (query.HasMoreResults)
                    {
                        var response = await query.ExecuteNextAsync<User>();
                        return response.FirstOrDefault(); //always returning null 
                    }
                }
                return null;

Now in the code above where no partition key is provided with cross partition query enabled it goes inside the if block but returning null all the time. To make sure that I have got the data in my collection, I checked the client query by doing the query.ToString() and ran the query in Azure DB explorer and it worked and returned the record but it is always returning null in my code above which tells me that something is wrong with the client or it is not working with cross partitioned query.

Code with Partition key

using (var query = _client.CreateDocumentQuery<User>(_documentCollectionUri,
                    new FeedOptions { EnableCrossPartitionQuery = true, PartitionKey = new PartitionKy(“myemail@email.com”) })
                                          .Where(u => u.Email == emailAddress.ToLower())
                                          .AsDocumentQuery())
                {
                    if (query.HasMoreResults)
                    {
                        var response = await query.ExecuteNextAsync<User>();
                        return response.FirstOrDefault(); //always returning null 
                    }
                }
                return null;

Now this code given above is working fine.

Code to get client instance is below

_client = new DocumentClient
                        (
                            new Uri(Configuration["azureSettings:documentDb:endPoint"]),
                            Configuration["azureSettings:documentDb:key"],
                            _jsonSerializerSettings,
                            new ConnectionPolicy
                            {
                                EnableEndpointDiscovery = false,
                                ConnectionMode = ConnectionMode.Direct,
                                ConnectionProtocol = Microsoft.Azure.Documents.Client.Protocol.Tcp,
                            }
                        );

After waiting for a while and reply from the Azure Cosmos DB team. I now understand why my fanout query was not working. All I have to do is change the code a little bit to make sure it will keep looking for the data until the end and then bail out with a result or no result. So to get it to work I have changed the code below

if (query.HasMoreResults)
{
   var response = await query.ExecuteNextAsync<User>();
   return response.FirstOrDefault(); //always returning null 
}

With the code

 var results = new List<MyClass>();
 while(query.HasMoreResults)
 {
     results.AddRange(await query.ExecuteNextAsync<MyClass>());
 }
 return results.FirstOrDefault(x => x.MyProp== propValue.ToLower());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM