-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from jiangz222/aggregate
support aggregate
- Loading branch information
Showing
17 changed files
with
385 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package qmgo | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type Pipeline []bson.D | ||
|
||
type Aggregate struct { | ||
ctx context.Context | ||
pipeline interface{} | ||
collection *mongo.Collection | ||
} | ||
|
||
// All iterates the cursor from aggregate and decodes each document into results. | ||
func (a *Aggregate) All(results interface{}) error { | ||
c, err := a.collection.Aggregate(a.ctx, a.pipeline) | ||
if err != nil { | ||
return err | ||
} | ||
return c.All(a.ctx, results) | ||
} | ||
|
||
// One iterates the cursor from aggregate and decodes current document into result. | ||
func (a *Aggregate) One(result interface{}) error { | ||
c, err := a.collection.Aggregate(a.ctx, a.pipeline) | ||
if err != nil { | ||
return err | ||
} | ||
cr := Cursor{ | ||
ctx: a.ctx, | ||
cursor: c, | ||
err: err, | ||
} | ||
defer cr.Close() | ||
if !cr.Next(result) { | ||
return ERR_NO_SUCH_RECORD | ||
} | ||
return err | ||
} | ||
|
||
// Iter return the cursor after aggregate | ||
func (a *Aggregate) Iter() CursorI { | ||
c, err := a.collection.Aggregate(a.ctx, a.pipeline) | ||
return &Cursor{ | ||
ctx: a.ctx, | ||
cursor: c, | ||
err: err, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package qmgo | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
func TestAggregate(t *testing.T) { | ||
ast := require.New(t) | ||
|
||
var cli *QmgoClient | ||
|
||
cli = initClient("test") | ||
cli.DropCollection(context.Background()) | ||
cli.EnsureIndexes(context.Background(), nil, []string{"name"}) | ||
|
||
id1 := primitive.NewObjectID() | ||
id2 := primitive.NewObjectID() | ||
id3 := primitive.NewObjectID() | ||
id4 := primitive.NewObjectID() | ||
id5 := primitive.NewObjectID() | ||
docs := []interface{}{ | ||
QueryTestItem{Id: id1, Name: "Alice", Age: 10}, | ||
QueryTestItem{Id: id2, Name: "Alice", Age: 12}, | ||
QueryTestItem{Id: id3, Name: "Lucas", Age: 33}, | ||
QueryTestItem{Id: id4, Name: "Lucas", Age: 22}, | ||
QueryTestItem{Id: id5, Name: "Lucas", Age: 44}, | ||
} | ||
cli.InsertMany(context.Background(), docs) | ||
matchStage := bson.D{{"$match", []bson.E{{"age", bson.D{{"$gt", 11}}}}}} | ||
groupStage := bson.D{{"$group", bson.D{{"_id", "$name"}, {"total", bson.D{{"$sum", "$age"}}}}}} | ||
var showsWithInfo []bson.M | ||
// aggregate ALL() | ||
err := cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}).All(&showsWithInfo) | ||
ast.NoError(err) | ||
ast.Equal(2, len(showsWithInfo)) | ||
for _, v := range showsWithInfo { | ||
if "Alice" == v["_id"] { | ||
ast.Equal(int32(12), v["total"]) | ||
continue | ||
} | ||
if "Lucas" == v["_id"] { | ||
ast.Equal(int32(99), v["total"]) | ||
continue | ||
} | ||
ast.Error(errors.New("error"), "impossible") | ||
} | ||
// Iter() | ||
iter := cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}) | ||
ast.NotNil(iter) | ||
err = iter.All(&showsWithInfo) | ||
ast.NoError(err) | ||
for _, v := range showsWithInfo { | ||
if "Alice" == v["_id"] { | ||
ast.Equal(int32(12), v["total"]) | ||
continue | ||
} | ||
if "Lucas" == v["_id"] { | ||
ast.Equal(int32(99), v["total"]) | ||
continue | ||
} | ||
ast.Error(errors.New("error"), "impossible") | ||
} | ||
// One() | ||
var oneInfo bson.M | ||
|
||
iter = cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}) | ||
ast.NotNil(iter) | ||
|
||
err = iter.One(&oneInfo) | ||
ast.NoError(err) | ||
ast.Equal(true, oneInfo["_id"] == "Alice" || oneInfo["_id"] == "Lucas") | ||
|
||
// iter | ||
iter = cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}) | ||
ast.NotNil(iter) | ||
|
||
i := iter.Iter() | ||
|
||
ct := i.Next(&oneInfo) | ||
ast.Equal(true, oneInfo["_id"] == "Alice" || oneInfo["_id"] == "Lucas") | ||
ast.Equal(true, ct) | ||
ct = i.Next(&oneInfo) | ||
ast.Equal(true, oneInfo["_id"] == "Alice" || oneInfo["_id"] == "Lucas") | ||
ast.Equal(true, ct) | ||
ct = i.Next(&oneInfo) | ||
ast.Equal(false, ct) | ||
|
||
// err | ||
ast.Error(cli.Aggregate(context.Background(), 1).All(&showsWithInfo)) | ||
ast.Error(cli.Aggregate(context.Background(), 1).One(&showsWithInfo)) | ||
ast.Error(cli.Aggregate(context.Background(), 1).Iter().Err()) | ||
matchStage = bson.D{{"$match", []bson.E{{"age", bson.D{{"$gt", 100}}}}}} | ||
groupStage = bson.D{{"$group", bson.D{{"_id", "$name"}, {"total", bson.D{{"$sum", "$age"}}}}}} | ||
ast.Error(cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}).One(&showsWithInfo)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.