Rakesh Kumar
Rakesh Kumar

Reputation: 3129

Cosmos DB - Getting error while creating a partition key "Cross partition query is required but disabled"

I am getting following error while creating a partition key in cosmos DB.

Exception while executing function: SetUserSubscriptions -> Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception.\r\nActivityId: 4685a5b7-bce9-4855-b2d8-33353f2957d9, Microsoft.Azure.Documents.Common/2.2.0.0, documentdb-dotnet-sdk/2.1.3 Host/32-bit MicrosoftWindowsNT/6.2.9200.0"

Here is my code:

public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByDesc,int takeCount =-1)
        {
            ;
            var criteria = client.CreateDocumentQuery<T>(
                    UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId))
                .Where(predicate)
                .OrderByDescending(orderByDesc)
                .AsDocumentQuery();

            IDocumentQuery<T> query = criteria;

            List<T> results = new List<T>();
            while (query.HasMoreResults)
            {
                if (takeCount>-1 && results.Count >= takeCount)
                {
                    break;
                }
                results.AddRange(await query.ExecuteNextAsync<T>());
            }

            return results;
        }

private static async Task CreateCollectionIfNotExistsAsync()
        {
            try
            {
                await client.ReadDocumentCollectionAsync(
                    UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId));
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    await client.CreateDocumentCollectionAsync(
                        UriFactory.CreateDatabaseUri(DatabaseId),
                        new DocumentCollection
                        {
                            Id = CollectionId,
                            IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 }),
                            PartitionKey = new PartitionKeyDefinition { Paths = new System.Collections.ObjectModel.Collection<string> { GetPartitionKeyAttributeCosmoDbCollection(typeof(T)) } }
                        },
                        new RequestOptions { OfferThroughput = 1000 });
                }
                else
                {
                    throw;
                }
            }
        }
        public static string GetPartitionKeyAttributeCosmoDbCollection(Type t)
        {
            // Get instance of the attribute.
            CosmoDbCollection attribute =
                (CosmoDbCollection)Attribute.GetCustomAttribute(t, typeof(CosmoDbCollection));

            if (attribute == null)
            {
                throw new Exception("The attribute CosmoDbCollection was not found.");
            }

            return attribute.PartitionKey;
        }

Upvotes: 0

Views: 307

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222552

As mentioned in the comment, you need to enable Cross partition query using Feed Options as follows,

  var criteria = client.CreateDocumentQuery<T>(
                    UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),new FeedOptions { EnableCrossPartitionQuery=true})

Upvotes: 1

Rakesh Kumar
Rakesh Kumar

Reputation: 3129

My issue got resolved when i added new FeedOptions { EnableCrossPartitionQuery=true}

public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByDesc, int takeCount = -1)
        {
            var criteria = client.CreateDocumentQuery<T>(
                    UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),new FeedOptions { EnableCrossPartitionQuery=true})
                .Where(predicate)
                .OrderByDescending(orderByDesc)
                .AsDocumentQuery();

            IDocumentQuery<T> query = criteria;

            List<T> results = new List<T>();
            while (query.HasMoreResults)
            {
                if (takeCount > -1 && results.Count >= takeCount)
                {
                    break;
                }
                results.AddRange(await query.ExecuteNextAsync<T>());
            }

            return results;
        }

Upvotes: 0

Related Questions