简体   繁体   中英

Deserialize cursor into array with mongo-go-driver and interface

I create an api using golang, i would like to create some functionnal test, for that i create an interface to abstract my database. But for that i need to be able to convert the cursor to an array without knowing the type.


func (self *KeyController) GetKey(c echo.Context) (err error) {
    var res []dto.Key
    err = db.Keys.Find(bson.M{}, 10, 0, &res)
    if err != nil {
        fmt.Println(err)
        return c.String(http.StatusInternalServerError, "internal error")
    }
    c.JSON(http.StatusOK, res)
    return
}
//THE FIND FUNCTION ON THE DB PACKAGE
func (s MongoCollection) Find(filter bson.M, limit int, offset int, res interface{}) (err error) {
    ctx := context.Background()
    var cursor *mongo.Cursor
    l := int64(limit)
    o := int64(offset)
    objectType := reflect.TypeOf(res).Elem()
    cursor, err = s.c.Find(ctx, filter, &options.FindOptions{
        Limit: &l,
        Skip:  &o,
    })

    if err != nil {
        return
    }

    defer cursor.Close(ctx)

    for cursor.Next(ctx) {
        result := reflect.New(objectType).Interface()
        err := cursor.Decode(&result)
        if err != nil {
            panic(err)
        }
        res = append(res.([]interface{}), result)

    }
    return
}


Does someone have an idea?

You can call directly the "All" method:

ctx := context.Background()
err = cursor.All(ctx, res)
if err != nil {
    fmt.Println(err.Error())
}

For reference:

https://godoc.org/go.mongodb.org/mongo-driver/mongo#Cursor.All

i think you want to encapsulate the Find method for mongo query.

Using the reflect package i have improved your code by adding an additional parameter that serves as a template to instantiate new instances of slice items.

func (m *MongoDbModel) FindAll(database string, colname string, obj interface{}, parameter map[string]interface{}) ([]interface{}, error) {
    var list = make([]interface{}, 0)

    collection, err := m.Client.Database(database).Collection(colname).Clone()

    objectType := reflect.TypeOf(obj).Elem()

    fmt.Println("objectype", objectType)
    if err != nil {
        log.Println(err)
        return nil, err
    }

    filter := bson.M{}

    filter["$and"] = []bson.M{}

    for key, value := range parameter {
        filter["$and"] = append(filter["$and"].([]bson.M), bson.M{key: value})
    }

    cur, err := collection.Find(context.Background(), filter)
    if err != nil {
        log.Fatal(err)
    }
    defer cur.Close(context.Background())
    for cur.Next(context.Background()) {
        result := reflect.New(objectType).Interface()
        err := cur.Decode(result)

        if err != nil {
            log.Println(err)
            return nil, err
        }

        list = append(list, result)
    }
    if err := cur.Err(); err != nil {
        return nil, err
    }
    return list, nil
}

The difference is that FindAll method returns []interface{} , where err := cur.Decode(result) directly consumes a pointer like the result variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM