Skip to content

Commit

Permalink
Makes code forward compatible up to 0.3.0
Browse files Browse the repository at this point in the history
Replaces nil documents with bson.D{{}}. Also removes accidental
renaming of options package. Fixes #2.
  • Loading branch information
tfogo committed Feb 8, 2019
1 parent beaecf5 commit bfa2f91
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ To find multiple documents, use `collection.Find()`. This method returns a `Curs

```go
// Pass these options to the Find method
options := options.Find()
options.SetLimit(2)
findOptions := options.Find()
findOptions.SetLimit(2)

// Here's an array in which you can store the decoded documents
var results []*Trainer

// Passing nil as the filter matches all documents in the collection
cur, err := collection.Find(context.TODO(), nil, options)
// Passing bson.D{{}} as the filter matches all documents in the collection
cur, err := collection.Find(context.TODO(), bson.D{{}}, findOptions)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -249,10 +249,10 @@ fmt.Printf("Found multiple documents (array of pointers): %+v\n", results)

### Delete Documents

Finally, you can delete documents using `collection.DeleteOne()` or `collection.DeleteMany()`. Here you pass `nil` as the filter argument, which will match all documents in the collection. You could also use [`collection.Drop()`](https://godoc.org/github.com/mongodb/mongo-go-driver/mongo#Collection.Drop) to delete an entire collection.
Finally, you can delete documents using `collection.DeleteOne()` or `collection.DeleteMany()`. Here you pass `bson.D{{}}` as the filter argument, which will match all documents in the collection. You could also use [`collection.Drop()`](https://godoc.org/github.com/mongodb/mongo-go-driver/mongo#Collection.Drop) to delete an entire collection.

```go
deleteResult, err := collection.DeleteMany(context.TODO(), nil)
deleteResult, err := collection.DeleteMany(context.TODO(), bson.D{{}})
if err != nil {
log.Fatal(err)
}
Expand Down
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,13 @@ func main() {

fmt.Printf("Found a single document: %+v\n", result)

// Add extra options to queries using the options package
options := options.Find()
options.SetLimit(2)
findOptions := options.Find()
findOptions.SetLimit(2)

var results []*Trainer

// Finding multiple documents returns a cursor
cur, err := collection.Find(context.TODO(), nil, options)
cur, err := collection.Find(context.TODO(), bson.D{{}}, findOptions)
if err != nil {
log.Fatal(err)
}
Expand All @@ -114,10 +113,11 @@ func main() {
fmt.Printf("Found multiple documents (array of pointers): %+v\n", results)

// Delete all the documents in the collection
deleteResult, err := collection.DeleteMany(context.TODO(), nil)
deleteResult, err := collection.DeleteMany(context.TODO(), bson.D{{}})
if err != nil {
log.Fatal(err)
}

fmt.Printf("Deleted %v documents in the trainers collection\n", deleteResult.DeletedCount)

// Close the connection once no longer needed
Expand Down

0 comments on commit bfa2f91

Please sign in to comment.