2

所以我正在尝试官方的 mongodb go 驱动程序(之前使用过 mgo),但我被卡住了。我正在尝试编组/解组自定义类型,默认情况下这显然不起作用。文档有点缺乏。基本上我想做的是:

type X struct {
    Y Z
}


type Z interface {
    ...
}

有没有办法做到这一点,或者它现在完全没有这个功能?我知道它仍然是一个测试版,但那里没有太多替代品。

编辑:

例如我想做的事情:

cur, err := mongo.Collection("coll").Find(ctx, bson.M{})
if err != nil {
  return err
}
defer cur.Close(ctx)
for cur.Next(ctx) {
  var el X
  err := cur.Decode(&el)
  if err == nil {
    // Do whatever...
  }
}
if err = cur.Err(); err != nil {
  return err
}

我得到的错误是:no decoder found for Z

测试示例:

package examples

import (
    "context"
    "testing"

    "github.com/mongodb/mongo-go-driver/bson"
    "github.com/mongodb/mongo-go-driver/mongo"
)

type X struct {
    Color string `json:"color" bson:"color"`
    Y     Z      `bson:"y"`
}

type Z interface {
    echo() string
}

func (x X) echo() string {
    return x.Color
}

func TestFindCars(t *testing.T) {
    var err error
    var client *mongo.Client
    var collection *mongo.Collection
    var cur mongo.Cursor
    var ctx = context.Background()
    if client, err = mongo.Connect(ctx, "mongodb://localhost/so?replicaSet=replset"); err != nil {
        t.Fatal(err)
    }
    collection = client.Database("so").Collection(collectionName)
    collection.InsertOne(ctx, bson.M{"color": "Red", "y": 12})
    if cur, err = collection.Find(ctx, bson.M{}); err != nil {
        t.Fatal(err)
    }
    if err != nil {
        t.Fatal(err)
    }
    defer cur.Close(ctx)
    for cur.Next(ctx) {
        var el X
        if err = cur.Decode(&el); err != nil {
            t.Fatal(err)
        }
        t.Log(el.echo())
    }
    if err = cur.Err(); err != nil {
        t.Fatal(err)
    }
}
4

1 回答 1

0

Here is a full example using your example and it ran fine.

package examples

import (
    "context"
    "testing"

    "github.com/mongodb/mongo-go-driver/bson"
    "github.com/mongodb/mongo-go-driver/mongo"
)

type X struct {
    Color string `json:"color" bson:"color"`
    Y     Z
}

type Z interface {
    echo() string
}

func (x X) echo() string {
    return x.Color
}

func TestFindCars(t *testing.T) {
    var err error
    var client *mongo.Client
    var collection *mongo.Collection
    var cur mongo.Cursor
    var ctx = context.Background()
    if client, err = mongo.Connect(ctx, "mongodb://localhost/so?replicaSet=replset"); err != nil {
        t.Fatal(err)
    }
    collection = client.Database("so").Collection(collectionName)
    collection.InsertOne(ctx, bson.M{"color": "Red"})
    if cur, err = collection.Find(ctx, bson.M{}); err != nil {
        t.Fatal(err)
    }
    if err != nil {
        t.Fatal(err)
    }
    defer cur.Close(ctx)
    for cur.Next(ctx) {
        var el X
        if err = cur.Decode(&el); err != nil {
            t.Fatal(err)
        }
        t.Log(el.echo())
    }
    if err = cur.Err(); err != nil {
        t.Fatal(err)
    }
}

Test

$ GOCACHE=off go test -v -run TestFindCars
=== RUN   TestFindCars
--- PASS: TestFindCars (0.09s)
    find2_test.go:49: Red
PASS
于 2019-01-26T11:41:11.320 回答