The val function doesn't work when i use upsert

Hi @Ro0tk1t,

Let’s assume that there is one node with name “Alice”. Assuming that you want to use an upsert block to copy name value, you can try this:

upsert{
  query{
      alice(func: eq(name,"Alice")){
        n as name
      }
      qName(){
        nm as max(val(n))
      }

  }
  mutation{
    set{
      _:new <name> val(nm) .
    }
  }
}

This will create a new node with the name set to “Alice”.

The reason we have to use a max aggregation here is because, there is no guarantee that the opt or the alice query returns a single result. What if the data is such that val(n) can have multiple values? The aggregation helps in this case.

2 Likes