I’m trying to count how many unique enteries in a database’s column… For example, If I have an entry for city, I want to return the number of different cities.
I would think to use .count() but how would “skip” cities entered more than once?
You need → .distinct() <—
Inside of your data-query.
import wixData from 'wix-data';
wixData.query("myCollection")
.distinct("state")
.then((results) => {
if(results.items.length > 0) {
let items = results.items;
let firstItem = items[0];
let totalCount = results.totalCount;
} else {
// handle case where no matching items found
}
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
});
this is what I have currently. I’ve counted many times, there are 8 cities and 10 shows up… @russian-dima
wixData.query("Faith-basedorganizations")
.distinct("city")
.then((results) => {
if (results.items.length > 0) {
let items = results.items;
let firstItem = items[0];
let totalCount = results.totalCount;
$w("#numCities").text = totalCount.toString();
} else {
// handle case where no matching items found
}
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
});