相关文章推荐
傻傻的凳子  ·  azul zulu OpenJDK 17 ...·  1 年前    · 
英勇无比的跑步鞋  ·  Spring Boot ...·  1 年前    · 

I have the following Mongo query I'm trying to translate to Go using github.com/globalsign/mgo :

db.getCollection('cluster').find({"clusterName": {"$in": ["clusterA", "clusterB"]}})

"clusterName" is a string field. Basically the naive alternative would be to perform multiple calls to mongo for each value in the list.

The query I wrote:

func ReadClusters(clusterNames []string) (*[]kusto.Cluster, error) {
    var clusters *[]kusto.Cluster
    err := readObjects(clusterCollection, bson.M{"clusterName": bson.M{"$in": clusterNames}}, &clusters, "" /* sortField */)
    if err != nil {
        return nil, err
    return clusters, nil

And my helper functions:

func readObjects(collection string, query bson.M, dest interface{}, sortField string) error {
    err := getDocuments(collection, query, dest, sortField)
    if err != nil {
        if err == mgo.ErrNotFound {
            return ErrNotFound
        return err
    return nil
func getDocuments(collectionName string, query bson.M, dest interface{}, sortField string) error {
    session := client.Copy()
    defer session.Close()
    collection := getCollection(session, collectionName)
    find := collection.Find(query)
    if sortField != "" {
        find = find.Sort(sortField)
    return find.All(dest)

I'm getting the error:

2020/07/09 11:58:46 http: panic serving [::1]:54085: result argument must be a slice address

I'm currently using Go1.11, and the mgo version I see under go.mod is github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8

Answers

clusters is already of pointer type to slice, so taking its address will be a pointer to pointer to slice.

Declare it to be a non-pointer to slice:

var clusters []kusto.Cluster
   
Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐

  • 浏览量 6
  • 收藏 0
  • 0

所有评论(0)