Grouping Data Properly

I have a collections called Messages, it contains a field inbox_id that are supposed to be conversation/chat threads. Some of the messages share the same inbox_id, but when I load the collection and filter by inbox_id, I can not group the individual messages to show a single repeater item to click to go to a dynamic page using the inbox_id as the field…

I have tried so many ways. I tried using aggregator data and the group method, but when I do this I lose all the other fields in the results.items list

const filter = wixData.filter().eq(“owner_ad”, user_id).or(
wixData.filter().eq(‘ad_owner’,user_id)
)

wixData.aggregate("Messages") 

//.filter(filter)
.group(“inbox_id”)
.run()
.then( (results) => {
console.log(results.items);

//$w(“#dataset1”).setFilter(results.items)
let data = [];
results.items.forEach((item) => {
let itemData = {“_id”:item._id}
data.push(itemData)
});

//console.log(data);
$w(“#repeater1”).data = results.items
})
. catch ((error) => {
console.log(error.message);
console.log(error.code);
});

I get this object array as a result

(2) […]

0: Object { inbox_id: “f5ae91a4-5918-d367-8f84-2ace0a68aaf9”, _id: “f5ae91a4-5918-d367-8f84-2ace0a68aaf9” }

1: Object { inbox_id: “2fa04b73-4499-625c-4d08-fd7bd882d886”, _id: “2fa04b73-4499-625c-4d08-fd7bd882d886” }
​length: 2

then when I remove the group filter I get (notice it includes all the items but is not grouping now, it has repeating inbox_id values and I get count of 4, when there is only 2 chats happening)

0: {…}
​​
_createdDate: Date Mon Nov 11 2019 21:25:40 GMT-0500 (Eastern Standard Time)
​​_id: “4ed72642-9c34-47e6-a3fe-15c5028a16a3”
​​_owner: “0fb277ef-c097-49e3-bf87-34b1cef08efd”
​​
_updatedDate: Date Mon Nov 11 2019 21:25:40 GMT-0500 (Eastern Standard Time)
​​ad_id: “a47a8f16-eb3d-17d3-14b5-60c47a6a2339”
​​inbox_id: “2fa04b73-4499-625c-4d08-fd7bd882d886”
​​message: “Please messsage me”
​​owner_ad: “0fb277ef-c097-49e3-bf87-34b1cef08efd”
​​sender: “FIrstName LastName”
​​title: “From example@gmail.com
​​
: Object { … }

1: Object { inbox_id: “2fa04b73-4499-625c-4d08-fd7bd882d886”, _id: “7e099933-3de5-47cc-98dc-c19a88d958e3”, _owner: “3410aa2c-dda1-4453-b141-5341d87c2e4e”, … }

2: Object { inbox_id: “f5ae91a4-5918-d367-8f84-2ace0a68aaf9”, _id: “cb071523-b246-46df-b542-d3aa3b8e322c”, _owner: “0fb277ef-c097-49e3-bf87-34b1cef08efd”, … }

3: Object { owner_ad: “0fb277ef-c097-49e3-bf87-34b1cef08efd”, inbox_id: “f5ae91a4-5918-d367-8f84-2ace0a68aaf9”, _id: “feacffe8-1264-4a18-afea-1d747009b256”, … }

PLEASE HELP

Anyone please?

Anyone?

Would really like some help with this please or some insights

Anyone?

Anyone?

Hello @James & @alinbaho

So aggregation is use to perform computation on sub dataset. So it will not be useful here.

why don’t you simply filter the data?

wixdata.query(“discussion”).eq(“owner_ad”, user_id).or(wixdata.query(“discussion”).eq(“owner_ad”, user_id));

That being said, maybe you need to change your data structure to better suit your usage

@plomteuxquentin is right. Using datasets for this particular task is only going to make your life harder. You can resolve this with a wixData query, but for performance and load times, organizing your data differently would be better.

Or if you want to brute force it the way you’re tyring to do it now, you’ll probably need JSON.parse() and learn how to use loops.