Moralis Query similar as Group By

Hello!

How can I query a table grouping by a unique value? I want to do something like (in relational db)

Select (field) from (table) group by (field).

To be more specific, I want to query BscTokenBalance and retrieve all registered tokens, but I want to receive only one item for each token. I tried

const query = new Moralis.Query("BscTokenBalance")
query.select("symbol");
query.distinct("symbol");
const resposta = await query.find();

But they donā€™t group with this query. Thx in advance!

you can search on how to do it with mongo db, also on how to do it with parse server, it may include a pipeline.

Thanks, Iā€™ll look for how to do this in mongo, but then I didnā€™t understand this distinct method. Could someone give an example of use? From the example in the documentation, I didnā€™t understand very well.

query.distinct may work only with master key, you could test it if it works

all it should to would be to keep only distinct entries, no group by

Okay, thanks, I think I understand now. About the original question, itā€™s easy to do using pipeline. I come from a background in SQL databases, I need to get used to noSql. Iā€™ll post the solution because maybe it will help someone at some point.

const query = new Moralis.Query(ā€œBscTokenBalanceā€)
query.select(ā€œsymbolā€);
const pipeline = [{
group: {
objectId: ā€œ$symbolā€
}
}];
const resposta = await query.aggregate(pipeline);

1 Like