A<->B many to many, query for all As with no Bs

Hello!

I’ve struggled with this for too long now. I haven’t been able to piece together the documentation for querying a many-to-many relationship for the records of one collection that have no relations through the “joins” collection.

I have songs, tags, and song_tags. I want to get all the songs that haven’t been tagged.

image

I think what I want to do is query for all the songs with an index, and then do a Difference of that and all the song ids present in the all_song_tags index. I’m still struggling to find the right FQL syntax for that mapping.

I’m also using the @relation resolver in GraphQL, if that makes a difference.

Thanks!

I think I figured it out.

I made a new index called allSongTagSongs.

{
  name: "allSongTagSongs",
  unique: true,
  serialized: true,
  source: "song_tags",
  values: [
    {
      field: ["data", "songID"]
    }
  ]
}

Then took the Difference from the allSongs index.

Difference(
  Match(Index("allSongs")),
  Distinct(Match(Index("allSongTagSongs")))
)

Using Distinct because it’s many-to-many so there are duplicate song refs in the index.

1 Like