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..7344280 100644 --- a/example_test.go +++ b/example_test.go @@ -102,7 +102,24 @@ func TestQmgo(t *testing.T) { } ast.Error(errors.New("error"), "impossible") } - //remove + // 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) + // 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)) + ast.Equal("", one.Name) + // remove err = cli.Remove(ctx, bson.M{"age": 7}) ast.Nil(err) }