azure-cosmosdbazure-sdk-.netlumenize

Need help on using documentdb-lumenize on .net documentdb client sdk


I have a problem using the aggregate storedproc lumenize https://github.com/lmaccherone/documentdb-lumenize with the .net client. I get error when try passing in the parameter and query into the storedproc. Below is my code

 public async static void QuerySP()        {
            using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey))
            {
                //Get the Database
                var database = client.CreateDatabaseQuery().Where(db => db.Id == databaseId).ToArray().FirstOrDefault();

                //Get the Document Collection
                var collection = client.CreateDocumentCollectionQuery(database.SelfLink).Where(c => c.Id == collectionId).ToArray().FirstOrDefault();

                StoredProcedure storedProc = client.CreateStoredProcedureQuery(collection.StoredProceduresLink).Where(sp => sp.Id == "cube").ToArray().FirstOrDefault();             


                dynamic result =  await client.ExecuteStoredProcedureAsync<dynamic>(storedProc.SelfLink, "{cubeConfig: {groupBy: 'publication', field: 'pid', f: 'count'}, filterQuery: 'SELECT pid, publication FROM c'}");
                Console.WriteLine("Result from script: {0}\r\n", result.Response);
            }
}

I am getting the following error when execute the code

Message: {"Errors":["Encountered exception while executing Javascript. Exception = Error: cubeConfig or savedCube required\r\nStack trace: Error: cubeConfig or savedCube required\n at fn (cube.js:1803:7)\n at __docDbMain (cube.js:1844:5)\n at Unknown script code (cube.js:1:2)"]}

Not sure what I had done wrong. I would really appreciate the help. Thanks.


Solution

  • You almost have it. The problem is that you are sending in the cubeConfig as a string. It needs to be an object. Here is code that does that:

    string cubeConfigString = @"{
        cubeConfig: {
            groupBy: 'publication', 
            field: 'pid', 
            f: 'count'
        }, 
        filterQuery: 'SELECT * FROM c'
    }";
    
    Object cubeConfig = JsonConvert.DeserializeObject<Object>(cubeConfigString);
    Console.WriteLine(cubeConfig);
    
    dynamic result = await client.ExecuteStoredProcedureAsync<dynamic>("dbs/dev-test-database/colls/dev-test-collection/sprocs/cube", cubeConfig);
    
    Console.WriteLine(result.Response);