Hey everyone,
I’ve got an issue, and I’ve seen some other posts with similar problems, but the solutions didn’t really apply to my use-case. I’ve been banging my head against my desk trying to solve it (amazing that that didn’t help), and so now I just need to see if anyone has any insight that could get me where I need to be.
I am building a function in which I require an array to be created that contains a list of items from only ONE field in a collection. I’m building a query to achieve that, and I thought using .distinct() would be my best bet, until I discovered that you can’t use the ascending/descending sorting with it (unless it’s the exact field you’re trying to isolate). I need to sort the array in an ascending order based on 2 different fields. One post I saw suggested using .find() and filtering the results. This seems like the direction I need to take, but I can’t quite make sense of how to apply it to my situation.
Here is what my failed code looks like with .distinct(), and yes I’m aware the ascending sorting won’t work here, but this is just to show what I am hoping to get out of this so that the correct solution might be offered:
import wixData from 'wix-data';
export function queryArrayTest() {
wixData.query("AllGuestUploads-Video")
.eq("eventCode", "abc123")
.ascending("chapterNumber", "orderInCategory")
.distinct("videoField")
.then( (results) => {
if (results.items.length > 0) {
let videoArray = results.items
console.log(videoArray)
}
})
}
I am trying to just get an array of only the “videoField”, in the ascending order that I showed there. Could someone offer a solution to how I could get my desired result? If it’s through filtering the results of a .find(), could I see how that might look in this use-case?
Thank you all for your time.
P.S. Everything with the above code works perfectly fine without the ascending sorting. When I run it, I get an array of only the videoField, which is great. But unfortunately I MUST have it sorted in ascending order based on those two other fields. Without that, the array is useless to me.
Hi @jbelt40
You can do so with Wix Data Aggregate . This function group() allows you to get that specific field and not the get other fields.
After you get the data, use Array.map() to retrieve the array of that field data from result.items
Does it helps?

I did try the aggregate function once, but I didn’t use .map(). I will attempt that now and return with the results. Thank you!
Okay, so I did aggregate again, and got things in order, but I honestly am not totally sure how to use Array.map() so I’m still missing that part. Here’s what my aggregate code looks like:
export function queryArrayTest() {
let filter = wixData.filter().eq("eventCode", "abc123");
wixData.aggregate("AllGuestUploads-Video")
.ascending("chapterNumber", "orderInCategory")
.filter(filter)
.group("videoField")
.run()
.then( (results) => {
let videoArray = results.items
console.log(videoArray)
})
}
And here’s what my result is just from that. I get an Array of objects in which there is the “videoField”, but also for some reason there’s an “_id” field in there that has the exact same data as the videoField, which is not right. The _id fields in that collection should be the standard wix IDs that get put in every entry automatically, not video URLs…So I have no idea what the deal is there…

BUT, the good news is that all of these videoField entries are sorted properly. Now I just need to get rid of the _id field and make this a single array of 32 videoField items instead of an array with 32 objects. I’m guessing this is where the Array.map comes in? Would you mind demonstrating to me how I might apply that here? I’m currently researching array.map, so if I get it figured out I will come back and update this with my results. But so far I am struggling to grasp how to get what I’m after using that.
Thank you!
Try this:
export function queryArrayTest() {
let filter = wixData.filter().eq('eventCode', 'abc123')
return wixData
.aggregate('AllGuestUploads-Video')
.ascending('chapterNumber', 'orderInCategory')
.filter(filter)
.group('videoField')
.run()
.then((results) => {
let videoArray = results.items
return videoArray.map(({ videoField }) => videoField)
})
}
That worked perfectly with one small change! Instead of “return videoArray.map” I had to do “let newArray = videoArray.map” to get the result for some reason. But that could’ve also been a failure on my part to properly do the console.log. With your code, I did console.log(“videoArray”, videoArray) and it gave the exact same results as what I got before. But this method worked perfectly:
export function queryArrayTest() {
let filter = wixData.filter().eq("eventCode", "tienken0515");
wixData.aggregate("AllGuestUploads-Video")
.ascending("chapterNumber", "orderInCategory")
.filter(filter)
.group("videoField")
.run()
.then((results) => {
let videoArray = results.items
let newArray = videoArray.map(({ videoField }) => videoField)
console.log("newArray", newArray) // This showed an array of just the videoField items.
})
}
The result is exactly what I need, just the video fields:
Thank you for the help @Bruno Prado & @Certified Code ! Lifesavers. 
Yeah, the results should be returned to a variable before being console.logged.
@bwprado got it, yeah that makes sense. I missed that lol