Is there is an easy way to get the count of elements from a column in database class?

I have a column in the Moralis dashboard which has a different string that is repeated.

I want to get the quantity of “number of times” each string value are repeated in the column in real time.

Is it possible?

You may be able to do something like this:

Moralis.Cloud.define("top_coins", async function(request){
  	const query = new Parse.Query("Coin");
    const pipeline = [
      {
        group:{
          objectId: "$name",
          count: { "$sum": 1 }
        }
         
      },
      {sort: {"total_sum": -1}},
      {limit: 100}
      ]
    const result = await query.aggregate(pipeline, {useMasterKey: true});
    return result;
});  

Can u elaborate on what exactly should I put in place of $name, $sum and “total_sum”?

For example, I have a column named “animals” and in this column, there are different animal names. Now how do I get the quantity of all animals or anyone of the animal?

you can use $animals instead of $name, in my case name was the name of the column. sum you let it that way, total_sum is only if you want them to be sorted.

It worked. I got the count of all animals in the animal column.

Is it possible to add one more column to filter in the same function?

for example, animals in the animal column are added by different uers. the usernames are stored in the UserName column.
So when I want to add filtering based on the username which should return me only animals entered by that particular user.

It should be possible, I don’t know the exact syntax now

1 Like

You can do something like this:

Moralis.Cloud.define("top_coins2", async function(request){
     const query = new Parse.Query("Coin");
    const pipeline = [
        
      { match: { objectId: "U8rr1Gn" } },
      {
        group:{
          objectId: "$name",
          count: { "$sum": 1 }
        }
         
      },
      {sort: {"total_sum": -1}},
      {limit: 100}
      ]
    const result = await query.aggregate(pipeline, {useMasterKey: true});
    return result;
});  
  

mainly I added a separate filter with match: { match: { objectId: "U8rr1Gn" } }
objectId was the column name in my case.