Order relateds depending on parent

Hi folks, how have you been?

Scenario:

  • I’m working on a music solution (spotify like) and I’m generating a RDF file in order to import via bulk load, then, I have SONG 1 that belongs to 2 ALBUMS (ALBUM 1 and ALBUM 2), mostly likely the track number won’t be the same in these albums. Currently I’m holding the track number in a “many to many table” SONG_X_ALBUMS(SONG_ID, ALBUM_ID) in a relational database.

Question:

The question is: How can I sort it by tracknumber based on the source (parent,origin, etc…) edge?

That is the RDF sample I’m generating:

<SONG1> <dgraph.type> "SongModel" .
<SONG1> <Explicit> "false"^^<xs:boolean> .
<SONG1> <ID> "1"^^<xs:integer> .
<SONG1> <ReleaseDate> "2000-07-01T00:00:00Z"^^<xs:date> .
<SONG1> <Duration> "241"^^<xs:integer> .
<SONG1> <Year> "2000"^^<xs:integer> .
<SONG1> <Views> "110"^^<xs:integer> .
<SONG1> <OriginalTitle> "Revolcón"^^<xs:string> .
<SONG1> <Title> "Revolcón"^^<xs:string> .
<SONG1> <UUID> "SONG1"^^<xs:string> .

<SONG2> <dgraph.type> "SongModel" .
<SONG2> <Explicit> "false"^^<xs:boolean> .
<SONG2> <ID> "2"^^<xs:integer> .
<SONG2> <ReleaseDate> "2000-07-01T00:00:00Z"^^<xs:date> .
<SONG2> <Duration> "241"^^<xs:integer> .
<SONG2> <Year> "2000"^^<xs:integer> .
<SONG2> <Views> "110"^^<xs:integer> .
<SONG2> <OriginalTitle> "Revolcón"^^<xs:string> .
<SONG2> <Title> "Revolcón"^^<xs:string> .
<SONG1> <UUID> "SONG2"^^<xs:string> .

<ALBUM1> <dgraph.type> "AlbumModel" .
<ALBUM1> <ID> "1"^^<xs:integer> .
<ALBUM1> <ReleaseDate> "2000-07-01T00:00:00Z"^^<xs:date> .
<ALBUM1> <Duration> "2597"^^<xs:integer> .
<ALBUM1> <Year> "2000"^^<xs:integer> .
<ALBUM1> <Views> "2330"^^<xs:integer> .
<ALBUM1> <OriginalTitle> "Champetas de Mi Pueblo, Vol. 2"^^<xs:string> .
<ALBUM1> <Title> "Champetas de Mi Pueblo, Vol. 2"^^<xs:string> .
<ALBUM1> <UUID> "ALBUM1"^^<xs:string> .

<ALBUM2> <dgraph.type> "AlbumModel" .
<ALBUM2> <ID> "2"^^<xs:integer> .
<ALBUM2> <ReleaseDate> "2018-07-06T00:00:00Z"^^<xs:date> .
<ALBUM2> <Duration> "222"^^<xs:integer> .
<ALBUM2> <Year> "2018"^^<xs:integer> .
<ALBUM2> <Views> "120"^^<xs:integer> .
<ALBUM2> <OriginalTitle> "When It Feels Right"^^<xs:string> .
<ALBUM2> <Title> "When It Feels Right"^^<xs:string> .
<ALBUM2> <UUID> "ALBUM2"^^<xs:string> .

<ALBUM1> <Song> <SONG2> . <-- song2 track number here is 1
<ALBUM1> <Song> <SONG1> . <-- song1 track number here is 2

<ALBUM2> <Song> <SONG1> . <-- song1 track number here is 1
<ALBUM2> <Song> <SONG2> . <-- song2 track number here is 2

I know it is possible to do something like

me(func: eq(UUID, "ALBUM1"))  { 
  Title
  songs (orderasc: ?) { <- order by tracknumber based on album1?
    uid
    Title
  }
}

tracknumber do you mean the ID predicate?

Why sorting? you can just put (orderasc: ID) right? or are you trying something else? Please, share an example of how it is now and how you would like to be the result of the query.

1 Like

Hi Michael,

I Suppose the returned values follows the order which they were inserted, right?

<ALBUM1> <Song> <SONG2> . 
<ALBUM1> <Song> <SONG1> . 
<ALBUM2> <Song> <SONG1> . 
<ALBUM2> <Song> <SONG2> . 

Which mean, if I query for ALBUM1 Song’s, it will follow the order SONG2, SONG1, right?

Sorry for the late response.

Nope, the mutations happen in a concurrent manner. So you won’t have ordered writing always. It can happen, but it is not guaranteed.

Yes and no, it depends on how fast you insert it. But you can order it via other predicates like “created_at” or something.