Skip to content

Commit

Permalink
Merge pull request #37 from jiangz222/updateone
Browse files Browse the repository at this point in the history
Updateone
  • Loading branch information
jiangz222 authored Aug 11, 2020
2 parents 67f9df6 + a4ec6d3 commit 21f2080
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}}}}
Expand Down
14 changes: 14 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}}}}
Expand Down
4 changes: 2 additions & 2 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -236,28 +236,28 @@ 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
update3 := bson.M{
"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)
}

Expand Down
19 changes: 18 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 21f2080

Please sign in to comment.