【问题标题】:How to delete quad in dgraph using dgo api.NQuad如何使用 dgo api.NQuad 在 dgraph 中删除四边形
【发布时间】:2018-09-02 23:54:45
【问题描述】:

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

我正在尝试实现相当于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,但这些解决方案都不起作用

【问题讨论】:

    标签: go dgraph dgraph-dgo


    【解决方案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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 2022-01-18
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多