Start a new topic
Answered

using GROUP collection to get list of all unique values

Hello,


I am Kinvey beginner. I have my collection, where each item/row within my collection has also value of one room where it belongs to. My point is to display a unique list of all used rooms in my collection. Plus to count how many particular rooms are in the collection. It leads to the using of Group method.

Here is how I use that for counting - it is working correctly with {room: body.room} parameter attached in the request body:


function onRequest(request, response, modules) {  

 var body = request.body; 

 modules.collectionAccess.collection('Example').group([], {room: body.room}, {"count":0}, "function (obj, prev) { prev.count++}", function (err, docs) {

  if (err) {

    logger.error('Query failed: '+ err);

  } else {

    response.body = docs;

    response.complete(200);

    }

  });

}


However I am suffering how to use GROUP method for how display a unique list of all used rooms in my collection... Any help would be more that appreciated!

Thank you


Best Answer

Pranav, 


thank you for your response. I read documentation about Kinvey Business Logic - Collection-Access-Module, there are only few words about Group method as a part of the CollectionAccess, and output using test API console gives only limited and general debug message "TypeError: undefined is not a function". However I found that method Distinct works well for this purpose. So if anybody wants to see/send list of all unique items in particular column (i.e. room) in the collection, here is the simple code to put as a part of Custom Endpoint:   


function onRequest(request, response, modules) {

  modules.collectionAccess.collection('Example').distinct('room', function (err, docs) {

   if (err) {

    logger.error('Query failed: '+ err);

  } else {

    response.body = docs;

    response.complete(200);

  }

    });

 }


output: 

["room1","room2","room3","room4"]

 

200 OK



Thanks,


Juraj


Juraj,


Please go through this documentation on Kinvey "User Groups". Please implement it on Kinvey's API console to get familiar with it.


Thanks,

Pranav



Answer

Pranav, 


thank you for your response. I read documentation about Kinvey Business Logic - Collection-Access-Module, there are only few words about Group method as a part of the CollectionAccess, and output using test API console gives only limited and general debug message "TypeError: undefined is not a function". However I found that method Distinct works well for this purpose. So if anybody wants to see/send list of all unique items in particular column (i.e. room) in the collection, here is the simple code to put as a part of Custom Endpoint:   


function onRequest(request, response, modules) {

  modules.collectionAccess.collection('Example').distinct('room', function (err, docs) {

   if (err) {

    logger.error('Query failed: '+ err);

  } else {

    response.body = docs;

    response.complete(200);

  }

    });

 }


output: 

["room1","room2","room3","room4"]

 

200 OK



Thanks,


Juraj

Login or Signup to post a comment