From e775b906b44ab40224daa67fe2af686a3a454f8f Mon Sep 17 00:00:00 2001 From: jiangzhi Date: Tue, 11 Aug 2020 20:59:20 +0800 Subject: [PATCH 1/3] updateone & readme --- README.md | 15 +++++++++++++++ README_ZH.md | 14 ++++++++++++++ collection.go | 4 ++-- collection_test.go | 12 ++++++------ example_test.go | 16 ++++++++++++++++ 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b7078f3..4cad4cf 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,21 @@ cli.Find(ctx, bson.M{"age": 6}).Sort("weight").Limit(7).All(&batch) ````go count, err := cli.Find(ctx, bson.M{"age": 6}).Count() ```` + +- Update +````go +// UpdateOne one +err := cli.UpdateOne(ctx, bson.M{"name": "d4"}, bson.M{"$set": bson.M{"age": 7}}) + +// UpdateAll +result, err := cli.UpdateAll(ctx, bson.M{"age": 6}, bson.M{"$set": bson.M{"age": 10}}) +```` + +- Select +````go +err := cli.Find(ctx, bson.M{"age": 10}).Select(bson.M{"age": 1}).One(&one) +```` + - Aggregate ```go matchStage := bson.D{{"$match", []bson.E{{"weight", bson.D{{"$gt", 30}}}}}} diff --git a/README_ZH.md b/README_ZH.md index 6689e1e..4455bd6 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -132,6 +132,20 @@ cli.Find(ctx, bson.M{"age": 6}).Sort("weight").Limit(7).All(&batch) count, err := cli.Find(ctx, bson.M{"age": 6}).Count() ```` +- Update +````go +// UpdateOne one +err := cli.UpdateOne(ctx, bson.M{"name": "d4"}, bson.M{"$set": bson.M{"age": 7}}) + +// UpdateAll +result, err := cli.UpdateAll(ctx, bson.M{"age": 6}, bson.M{"$set": bson.M{"age": 10}}) +```` + +- Select +````go +err := cli.Find(ctx, bson.M{"age": 10}).Select(bson.M{"age": 1}).One(&one) +```` + - Aggregate ```go matchStage := bson.D{{"$match", []bson.E{{"weight", bson.D{{"$gt", 30}}}}}} diff --git a/collection.go b/collection.go index 1e29d45..fe5a3b9 100644 --- a/collection.go +++ b/collection.go @@ -57,9 +57,9 @@ func (c *Collection) Upsert(ctx context.Context, filter interface{}, replacement return } -// Update executes an update command to update at most one document in the collection. +// UpdateOne executes an update command to update at most one document in the collection. // Reference: https://docs.mongodb.com/manual/reference/operator/update/ -func (c *Collection) Update(ctx context.Context, filter interface{}, update interface{}) error { +func (c *Collection) UpdateOne(ctx context.Context, filter interface{}, update interface{}) error { var err error var res *mongo.UpdateResult diff --git a/collection_test.go b/collection_test.go index c257103..6796aa2 100644 --- a/collection_test.go +++ b/collection_test.go @@ -223,7 +223,7 @@ func TestCollection_Update(t *testing.T) { "age": 18, }, } - err = cli.Update(context.Background(), filter1, update1) + err = cli.UpdateOne(context.Background(), filter1, update1) ast.NoError(err) // error when not exist @@ -236,7 +236,7 @@ func TestCollection_Update(t *testing.T) { "age": 20, }, } - err = cli.Update(context.Background(), filter2, update2) + err = cli.UpdateOne(context.Background(), filter2, update2) ast.Equal(err, ErrNoSuchDocuments) // filter is nil or wrong BSON Document format @@ -244,20 +244,20 @@ func TestCollection_Update(t *testing.T) { "name": "Geek", "age": 21, } - err = cli.Update(context.Background(), nil, update3) + err = cli.UpdateOne(context.Background(), nil, update3) ast.Error(err) - err = cli.Update(context.Background(), 1, update3) + err = cli.UpdateOne(context.Background(), 1, update3) ast.Error(err) // update is nil or wrong BSON Document format filter4 := bson.M{ "name": "Geek", } - err = cli.Update(context.Background(), filter4, nil) + err = cli.UpdateOne(context.Background(), filter4, nil) ast.Error(err) - err = cli.Update(context.Background(), filter4, 1) + err = cli.UpdateOne(context.Background(), filter4, 1) ast.Error(err) } diff --git a/example_test.go b/example_test.go index d049c7e..175c5ee 100644 --- a/example_test.go +++ b/example_test.go @@ -102,6 +102,22 @@ func TestQmgo(t *testing.T) { } ast.Error(errors.New("error"), "impossible") } + // Update one + err = cli.UpdateOne(ctx, bson.M{"name": "d4"}, bson.M{"$set": bson.M{"age": 17}}) + ast.NoError(err) + cli.Find(ctx, bson.M{"age": 17}).One(&one) + ast.Equal("d4", one.Name) + // UpdateAll + result, err := cli.UpdateAll(ctx, bson.M{"age": 6}, bson.M{"$set": bson.M{"age": 10}}) + ast.NoError(err) + count, err = cli.Find(ctx, bson.M{"age": 10}).Count() + ast.NoError(err) + ast.Equal(result.ModifiedCount, count) + one = UserInfo{} + err = cli.Find(ctx, bson.M{"age": 10}).Select(bson.M{"age": 1}).One(&one) + ast.NoError(err) + ast.Equal(10, int(one.Age)) + ast.Equal("", one.Name) //remove err = cli.Remove(ctx, bson.M{"age": 7}) ast.Nil(err) From 3158480791b25a8c01985401aab6340eff1ffc50 Mon Sep 17 00:00:00 2001 From: jiangzhi Date: Tue, 11 Aug 2020 21:00:20 +0800 Subject: [PATCH 2/3] updateone & readme --- example_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/example_test.go b/example_test.go index 175c5ee..c2831af 100644 --- a/example_test.go +++ b/example_test.go @@ -114,11 +114,12 @@ func TestQmgo(t *testing.T) { ast.NoError(err) ast.Equal(result.ModifiedCount, count) one = UserInfo{} + // select err = cli.Find(ctx, bson.M{"age": 10}).Select(bson.M{"age": 1}).One(&one) ast.NoError(err) ast.Equal(10, int(one.Age)) ast.Equal("", one.Name) - //remove + // remove err = cli.Remove(ctx, bson.M{"age": 7}) ast.Nil(err) } From a4ec6d302cf28dc3f0cd7108aaafd90ff455c25e Mon Sep 17 00:00:00 2001 From: jiangzhi Date: Tue, 11 Aug 2020 21:00:36 +0800 Subject: [PATCH 3/3] updateone & readme --- example_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_test.go b/example_test.go index c2831af..7344280 100644 --- a/example_test.go +++ b/example_test.go @@ -113,8 +113,8 @@ func TestQmgo(t *testing.T) { count, err = cli.Find(ctx, bson.M{"age": 10}).Count() ast.NoError(err) ast.Equal(result.ModifiedCount, count) - one = UserInfo{} // select + one = UserInfo{} err = cli.Find(ctx, bson.M{"age": 10}).Select(bson.M{"age": 1}).One(&one) ast.NoError(err) ast.Equal(10, int(one.Age))