0

I was going through kinvey (BAAS) documentation and I came across Aggregation concept using Map/reduce.

http://docs.kinvey.com/rest-aggregation.html

Since Kinvey does not have the .Net SDK, the only option that we have is to use the Rest API. I am familiar with REST API, but , as per the documentation (link above), but, How do I construct the Map/Reduce syntax in the API request parameters in the C# backend code ?

The windows Phone app , that we would like to make will have C# Code in the back end. and there are couple of aggregation modules/Data points that we would like to display in the app. I wanted to utilize the Map/reduce functionality provided by kinvey as it runs on MongoDB, rather than using linq and doing the processing in the code.

I would appreciate if someone could shed some light on it.

Thanks Rahul

Rahul
  • 95
  • 1
  • 2
  • 13

1 Answers1

1

You want to send a POST request to /appdata/:appKey/:collectionName/_group, where :appkey is the app id and :collectionName is the particular collection you want to run the aggregation on.

Then in the body, you want to specify the map/reduce parameters:

{
  "key": {
    "lastName":true
  },
  "initial": {
    "count": 0
  },
  "reduce": "function(doc,out){ out.count++;}",
  "condition": {
    "age": { "$gt":31 }
  }
}

In this example, the "key" specifies the entity column names that will be aggregated. Most of the time you'll just want to do one, but you can specify multiple. "initial" seeds the reduce starting value, since this is a counting example, it's started at zero. The "reduce" is a javascript function; it passes in the entity (doc) and our return object (out). If you wanted to sum everyone's age instead of the number of people, it would like: out.count += doc.age. Finally you can specify an optional "condition"; this is a filter using the query language, only entities that satisfy the condition will be evaluated.

For reference, here is the MongoDB aggregation documentation. You should be able to everything listed in there http://www.mongodb.org/display/DOCS/Aggregation

And the Kinvey documentation for the query syntax: http://docs.kinvey.com/rest-filtering.html (remember this is part of the condition in the body object, not in the POST uri).

In case you need it, here's a question about converting a Dicitonary to JSON. How do I convert a dictionary to a JSON String in C#?

Community
  • 1
  • 1
Mike Katz
  • 2,060
  • 2
  • 17
  • 25
  • Thanks a lot for the clarification. Just for clarification , per the Kinvey documentation, it suggests, putting the filtering criteria in the POST URI, so do you think putting the filtering in the BODY along with the aggregation is better. Thanks – Rahul Sep 15 '12 at 15:56
  • 1
    For aggregation, I would recommend putting the filter as the condition argument in the body. – Mike Katz Sep 20 '12 at 16:39