Hi,
I have a database collection, I need to query it so i can retrieve a count of rows by name
in other words, i’d like to get the amount of rows per distinct value in column name
in relational databases it is equivalent to group by
or at least retrieving unique rows according to a field value
Hello Ana,
To do this you will have to query your collection for a specific item, get the values of the object that is returned, and then for each value push it into an array if it is not already in it.
It would look something like this
wixData.query('CollectionName')
.eq('name', 'exampleName')
.find()
.then((result) => {
//get specified item
let uniqueArray = [];
//get the values of the result object
let values = Object.values(result.items[0])
//for each object, check if its in array, if it isnt push it
values.forEach((e, i)=> {
if(!uniqueArray.includes(e)){
uniqueArray.push(e);
}
});
//prints unique array and its length here
console.log(uniqueArray, uniqueArray.length);
}).catch((err) => {
console.log(err)
});
Goodluck!
Hi,
I do not know the different values that exists in “name” (as per your example)
In order to implement your recommendation (as in your example), I need to get all the “name” values first as distinct values. Is it possible to do that?
What is the WIX equivalent to relational databases query with DISTINCT or GROUP BY ?
Apologies for the late reply,
In Wix there is sorting and filtering . as for the query options, here is the full list:
‘name’ is only a field in your database, in the example above I create an array called uniqueArray and only push to it if the value is not found in it.
Goodluck,
Majd
You might find WixDataAggregate.group() and the other aggregate functions useful.