Update list of uids

I am trying to update a list of uids in Dgraph via DQL.

uidList: [uid] .

type DummyType {
uidList
}

So for example I want to update an already existing uid list (predicate uidList) consisting of 0x1, 0x2, 0x3 to 0x5, 0x6, 0x7, 0x8.

I am using the Golang client Dgo. Is the above possible in one transaction/call? Currently I have to first delete the existing list and after that set the new elements.

  1. If you know the UID of the target entity.

Do the mutation with its uid.

<0x0fff34> <uidList> <0x5>  .
<0x0fff34> <uidList> <0x6>  .
<0x0fff34> <uidList> <0x7>  .
<0x0fff34> <uidList> <0x8>  .
  1. If you don’t know the UID but you know how to find it.
upsert {
  query {
     v as q(func: eq(username, "NAME))
  }

  mutation {
    set {
      uid(v) <uidList> <0x5>  .
      uid(v) <uidList> <0x6>  .
      uid(v) <uidList> <0x7>  .
      uid(v) <uidList> <0x8>  .
    }
  }
}
  1. f you wanna remove the 0x1, 0x2, 0x3.
{ 
  delete   {
      <0x0fff34> <uidList> <0x1>  .
      <0x0fff34> <uidList> <0x2>  .
      <0x0fff34> <uidList> <0x4>  .
   }
}

And now

{ 
  set   {
       <0x0fff34> <uidList> <0x5>  .
       <0x0fff34> <uidList> <0x6>  .
       <0x0fff34> <uidList> <0x7>  .
       <0x0fff34> <uidList> <0x8>  .
   }
}
  1. f you wanna remove the 0x1, 0x2, 0x3 and set the new relations, but you don’t know the uid.
upsert {
  query {
     v as q(func: eq(username, "NAME))
  }

  mutation {
    set {
      uid(v) <uidList> <0x5>  .
      uid(v) <uidList> <0x6>  .
      uid(v) <uidList> <0x7>  .
      uid(v) <uidList> <0x8>  .
    }
    delete   {
      uid(v) <uidList> <0x1>  .
      uid(v) <uidList> <0x2>  .
      uid(v) <uidList> <0x4>  .
   }
  }
}
2 Likes

Thank you for the help. I tested your code and it works as expected.

Is it also possible to delete all edges without knowing the uids and after that insert the new edges? I tried the mutation below, but it seems the delete block is executed after the set block, so I end up with no edges at all.

upsert {
  query {
     v as q(func: eq(username, "NAME))
  }

  mutation {
    delete   {
      uid(v) <uidList> * .
    }
    set {
      uid(v) <uidList> <0x5>  .
      uid(v) <uidList> <0x6>  .
      uid(v) <uidList> <0x7>  .
      uid(v) <uidList> <0x8>  .
    }
  }
}
1 Like

Humm, not sure. Feels like the order of the operations doesn’t matter. You can do in two rounds.

You can also try.

upsert {
  query {
     v as q(func: eq(username, "NAME"))
  }
  mutation {
    delete   {
      uid(v) <uidList> * .
    }
  }
  mutation {
    set {
      uid(v) <uidList> <0x5>  .
      uid(v) <uidList> <0x6>  .
      uid(v) <uidList> <0x7>  .
      uid(v) <uidList> <0x8>  .
    }
  }
}
1 Like

That worked, thank you.