Trying to sort multiple groups without success

Hello, I am a newbie to mongodb and I am trying to sort venues by name and performers by name in ascending order in the following query after the group stage so results are consistent, as of now I am able to sort by event start date which seems consistent every single time I run the query, I have tried adding sort to this query after the group stage with no success I have also tried to sort after the $project stage, however this feels incorrect since I’m slicing the results in the $project stage, I have also tried to sort inside the lookup pipelines. I really don’t know what else to try, I was reading a bit about facets but I’m not sure that it’s what I want?

Another issue that I’m having is that sometimes I get records in venues which are empty, i.e:

,
[{ actual venue data}]

This issue is secondary, I’ve been busting my head all day to try to sort this query correctly.

Query:

Sample data can be found here:

Thanks in advance

Hello @Federico_Stange .

Welcome to The MongoDB Community Forums! :wave:

Could you please share below details to understand your use-case better:

  • What is the expected output from your query?
  • Do you want to involve records with array value of venue as null?

Regards,
Tarun

Hello I dont want empty records, the resulting records should look a bit like the following, I had to unwind and then group again for the sorting to take place, which I think it’s not ideal but at least it works.

In this case I looked up for bee, so it matched all performers with .bee. all events containing .bee. and all venues containing .bee., to get rid of the empty records I tried to $match $ne on the record, but then I realized that this would only bring results if ALL the subdocuments would match .bee., in this case as we have all records containing .bee. it would bring the result without the empty records, however if I would search for a venue .*sunshine.grove. using $match after the $unwind stage gets me no records because there are no performers matching .*sunshine.grove. however I do need that the result comes back as a venue, even tho there are no performers named .*sunshine.grove.

Additionaly to try to match for non empty records after $unwind, I have tried to filter those out in the $group stage with no success. Here is the updated query:

This is the result set, in this case it’s correct, minus the empty record in performer :frowning:

[
  {
    performers: [
      [],
      [
        {
          _id: 701,
          name: 'Atlantic City Beer & Music Festival',
          segment: {
            name: 'Music',
            uid: 'e062293a-9b46-11ed-8aab-0242ac130005'
          },
          bio: null,
          homepage: 'https://www.acbeerfest.com/',
          uid: 'f35adf69-1cb0-41d6-91ab-9cb330566c91',
          genre: {
            uid: '506a0d84-d310-471f-8d56-b863f91dc69b',
            name: 'Undefined'
          },
          type: [],
          subtype: [],
          websites: {
            facebook: 'https://www.facebook.com/acbeerfest/',
            instagram: 'https://www.instagram.com/acbeerfest/',
            twitter: 'https://twitter.com/acbeerfest'
          }
        }
      ],
      [
        {
          _id: 855,
          name: 'Beenie Man',
          segment: {
            name: 'Music',
            uid: 'e062293a-9b46-11ed-8aab-0242ac130005'
          },
          bio: null,
          homepage: 'http://www.beenieman.net/',
          uid: 'cd19510e-f7d4-4f30-a177-eb3382c95fdf',
          genre: {
            uid: '152b15b3-91d4-11ed-a212-0242ac130003',
            name: 'Reggae'
          },
          type: [],
          subtype: [],
          websites: {
            lastfm: 'http://www.last.fm/music/Beenie+Man',
            musicbrainz: '0d85b9f2-802d-48bb-aa85-6a9668869053',
            wiki: 'https://en.wikipedia.org/wiki/Beenie_Man'
          }
        }
      ]
    ],
    events: [
      {
        uid: '4199441d-2c40-4cac-936a-4c357ba83d69',
        title: 'Okeechobee Music & Arts Festival',
        type: 'Festival',
        start: ISODate("2023-03-02T00:00:00.000Z"),
        performers: {
          names: [
            'Austin Millz',
            'Baby Keem',
            'Big Boi',
            'Biig Piig',
            'Blunts & Blondes',
            'Boogie T',
            'Break Science'
          ],
          total: 62
        },
        venue: 'Sunshine Grove',
        country: 'United States',
        state: 'Florida',
        city: 'Okeechobee'
      },
      {
        uid: 'e46e3dc8-1327-4130-aab3-f09c250e54d0',
        title: "PSW & Pherm Brewing present a night of Phun with The Last Rwind (DC's Phish Tribute), Pherm Brewing Beer specials and give aways",
        type: 'Generic',
        start: ISODate("2023-03-11T01:00:00.000Z"),
        performers: { names: [], total: 0 },
        venue: 'Pearl Street Warehouse',
        country: 'United States',
        state: 'District of Columbia',
        city: 'Washington'
      },
      {
        uid: '870dac81-5964-48dd-8bc8-d830f29bc440',
        title: 'Decibel Metal & Beer Fest - 2 Day Pass',
        type: 'Generic',
        start: ISODate("2023-04-14T00:00:00.000Z"),
        performers: {
          names: [
            'All Out War',
            'Decibel Metal & Beer Fest',
            'Drowning Man',
            'Escuela Grind',
            'Eyehategod',
            'Frozen Soul',
            'Fuming Mouth'
          ],
          total: 15
        },
        venue: 'The Fillmore Philadelphia',
        country: 'United States',
        state: 'Pennsylvania',
        city: 'Philadelphia'
      }
    ],
    venues: [ [] ]
  }
]