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