get data from cosmos db in c# for given querystring

Viewed 1475

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

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.

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;
        }
Related