2

I have the following function which is fetching whole documents :

public async Task<IEnumerable<dynamic>> GetAllPreferences(string queryString)
        {
            IDocumentQuery<dynamic> query = documentClient.CreateDocumentQuery<dynamic>(
                UriFactory.CreateDocumentCollectionUri(collectionInfo.DatabaseName, collectionInfo.CollectionName))
                .AsDocumentQuery();

            List<dynamic> results = new List<dynamic>();
            while (query.HasMoreResults)
            {
                results.AddRange(await query.ExecuteNextAsync<dynamic>());
            }

            return results;
        }

But here I am not able to make use of queryString. I have the following structure of my document :

{
    "type": [
        "university",
        "institute"
    ],
    "preferences": [
        {
            "type": "university",
            "universityDetail": [
                .....
            ],
            "state" : [
                ....
            ]
        },
        {
            "type": "institute",
            "instituteDetail": [
                .....
            ],
            "state" : [
                ....
            ]
        }
    ]
}

I have to get only preferences from each document. The equivalent sql query is "select preferences from collectionInfo.CollectionName".

Please suggest the code edit and also help me with the exact queryString required in my case.

Thanks a lot in Advance.

2 Answers 2

1

You could try this code using your SDK:

public async Task<IEnumerable<dynamic>> GetAllPreferences(string queryString)
        {
            SqlQuerySpec queryStr = new SqlQuerySpec(queryString); //"select c.preferences from c"
            FeedOptions feedOptions = new FeedOptions();
            feedOptions.EnableCrossPartitionQuery = true;

            IDocumentQuery<dynamic> query = documentClient.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(collectionInfo.DatabaseName, collectionInfo.CollectionName), queryString, feedOptions).AsDocumentQuery();

            List<dynamic> results = new List<dynamic>();
            while (query.HasMoreResults)
            {
                results.AddRange(await query.ExecuteNextAsync<dynamic>());
            }

            return results;
        }
Sign up to request clarification or add additional context in comments.

Comments

1

Please try this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Linq;

namespace SO67598286
{
    class Program
    {
        const string connectionString = "connection-string";
        const string databaseId = "database-name";
        const string containerId = "container-name";
        static async Task Main(string[] args)
        {
            CosmosClient client = new CosmosClient(connectionString);
            Container container = client.GetContainer(databaseId, containerId);
            var query = container.GetItemLinqQueryable<IDictionary<string, object>>()
                .Select(f => f["preferences"]);
            var iterator = query.ToFeedIterator();
            while (iterator.HasMoreResults)
            {
                foreach (var document in await iterator.ReadNextAsync())
                {
                    Console.WriteLine(document.ToString());
                }
            }
        }
    }
}

Please note that this code makes use of Microsoft.Azure.Cosmos Nuget package and I believe you're using an older version of Cosmos DB SDK.

Also, please see this post for more details: https://blog.jeremylikness.com/blog/using-linq-to-query-dynamic-schemaless-cosmosdb-documents/. I have used the code mentioned in this blog post only.

1 Comment

I dont have connection string, I have been given the DocumentClient object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.