# Accessing data from database via Aggregation

**URL:** <https://forum.wixstudio.com/t/accessing-data-from-database-via-aggregation/10925>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [January 4, 2022, 7:21pm UTC](https://forum.wixstudio.com/t/accessing-data-from-database-via-aggregation/10925 "2022-01-04T19:21:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephenmccall](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stephenmccall/32/1773_2.png) [@stephenmccall](https://forum.wixstudio.com/u/stephenmccall)\
**Post date:** [January 4, 2022, 7:21pm UTC](https://forum.wixstudio.com/t/accessing-data-from-database-via-aggregation/10925/1 "2022-01-04T19:21:21Z")

</div>

Hi folks,

I have been playing with the aggregation and have what I’m looking to achieve but I feel that there must be a cleaner way to achieve what I’m doing. Basically I want to count the participants for a specified item that are on the same date.

I.e. Item 1, 25th Jan, 4 people

I have this working fine and get the expected results. My question is, how is it best to get other data from the database to work with this in the array? The code is below

```javascript
async function getUniqueItems() {

    const filter = wixData.filter().between('date', new Date, endDate).ne('minimumParticipants', "Private");
    let data = await wixData.aggregate("CourseAvailability").group("title", "date")
    .ascending("date").filter(filter).max('maxParticipants').min('courseCategory').sum('numberOfParticipants').run();
    
    let items = data.items;
    
    items.forEach((el, i) => items[i]._id = gettRandomID());
    console.log(items)

    $w('#repeater1').data = items;
    $w("#repeater1").onItemReady(($item, itemData, index) => { 
    
        if(itemData.numberOfParticipantsSum >= itemData.maxParticipantsMax) $item('#container1').collapse();
        
        $item("#courseImage").src = itemData.image;
        $item('#courseImage').tooltip = itemData.title;
        $item("#courseTitle").text = itemData.title.toUpperCase();
        $item('#date').text = itemData.date.toDateString();
        $item('#courseInfoButton').onClick((event) => {
            wixLocation.to("/courses/" + encodeURI(itemData.courseCategoryMin) + "/" + encodeURI(itemData.title))
            $item('#courseInfoButton').label = "Loading...";
        });

    });
    
}

```

As you can see I have used the Min to access some other info that I want to use from the database. I would also like to use the ‘image’ field from the database. What is the best method to achieve this? Do I need to run a sperate query? It seems like I should be able to access all of the database field keys but that’s the area I’m struggling with.

Also, I have an issue where the title has spaces so I had to use a randomId to let me build the array

This photo may help with clarity

 ![](https://us1.discourse-cdn.com/wix/original/3X/5/1/51c140b0e4ef3a8f052cff56c992e28d9f54c3d1.png)

Thanks as always!

---

<div class="post-metadata">

**Author:** ![stephenmccall](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stephenmccall/32/1773_2.png) [@stephenmccall](https://forum.wixstudio.com/u/stephenmccall)\
**Post date:** [January 17, 2022, 11:19am UTC](https://forum.wixstudio.com/t/accessing-data-from-database-via-aggregation/10925/2 "2022-01-17T11:19:30Z")

</div>

Would anyone be able to offer some help? It’s not very important more that I’m trying to improve my coding skills 🙂
