MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

So geben Sie mehrere Werte mit Go Mongo Distinct zurück

Nach prasad Kommentar habe ich mein Problem mit dieser Lösung gelöst.

    type Example struct {}

    var exm []Example
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    pipeline := []bson.M{
        {"$match": bson.M{"status": "Pending"}},
        {"$group": bson.M{"_id": "$batch"}},
    }

    cursor, err := db.Collection("xyzcollection").Aggregate(ctx, pipeline)
    if err != nil {
        return []Example{}, errors.New(fmt.Sprintf("unable to retrive data: %s ", err.Error()))
    }

    var result Example
    for cursor.Next(ctx) {
        cursor.Decode(&result)
        exm = append(exm, result)
    }
    return exm, nil