如何使用dgo api.NQuad删除dgraph中的四边形

时间:2018-08-09 09:21:13

标签: go dgraph dgraph-dgo

是否可以使用api.NQuad中的github.com/dgraph-io/dgo/protos/api从给定节点中删除所有与谓词匹配的边?

我正在尝试达到delete {0x1234 <test.likes> * }的水平

func TestDeleteQuad(t *testing.T) {
    subject := "0x01"
    ctx := context.TODO()
    d, err := grpc.Dial(testEndpoint, grpc.WithInsecure())
    if err != nil {
        panic(err)
    }
    client := dgo.NewDgraphClient(api.NewDgraphClient(d))
    txn := client.NewTxn()
    defer txn.Discard(ctx)
    if _, err := txn.Mutate(ctx, &api.Mutation{Del: []*api.NQuad{
        &api.NQuad{
            Subject:     subject,
            Predicate:   "test.likes",
            ObjectId:    "*",
            ObjectValue: nil,
        },
    }}); nil != err {
        panic(err)
    }
    err = txn.Commit(ctx)
    assert.NoError(t, err)
}

我尝试使用"*" "" x.Star作为ObjectId,但是这些解决方案均无效

1 个答案:

答案 0 :(得分:1)

这是非常直观的,但是要删除边,必须使用ObjectValue而不是将ObjectId设置为api.Value_DefaultVal星号:

func TestDeleteQuad(t *testing.T) {
    subject := "0x01"
    ctx := context.TODO()
    d, err := grpc.Dial(testEndpoint, grpc.WithInsecure())
    if err != nil {
        panic(err)
    }
    client := dgo.NewDgraphClient(api.NewDgraphClient(d))
    txn := client.NewTxn()
    defer txn.Discard(ctx)
    if _, err := txn.Mutate(ctx, &api.Mutation{Del: []*api.NQuad{
        &api.NQuad{
            Subject:     subject,
            Predicate:   "test.likes",
            ObjectValue: &api.Value{&api.Value_DefaultVal{x.Star}}, // <- this
        },
    }}); nil != err {
        panic(err)
    }
    err = txn.Commit(ctx)
    assert.NoError(t, err)
}