-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4d6e1a
commit 27c4342
Showing
2 changed files
with
73 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
- [ ] Implement History | ||
- [ ] gomongo.Options | ||
- [ ] Implement Validation Hooks | ||
- [ ] Implement Cache | ||
- [ ] Add Benchmarks | ||
- [ ] Add Badges to README | ||
- [ ] Add Index instructions |
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,66 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/victorguarana/gomongo/gomongo" | ||
Check failure on line 8 in main.go GitHub Actions / Tests (4.4)
Check failure on line 8 in main.go GitHub Actions / Tests (5)
|
||
) | ||
|
||
type Movie struct { | ||
ID gomongo.ID `bson:"_id"` | ||
Name string | ||
Year int | ||
} | ||
|
||
func main() { | ||
// Setting up the connection to the database | ||
connectionSettings := gomongo.ConnectionSettings{ | ||
URI: "mongodb://localhost:27017", | ||
DatabaseName: "mydatabase", | ||
ConnectionTimeout: 60 * time.Second, | ||
} | ||
database, err := gomongo.NewDatabase(context.Background(), connectionSettings) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Creating a collection | ||
moviesCollection, err := gomongo.NewCollection[Movie](database, "mymovies") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Inserting a movie | ||
starWarsIV := Movie{ | ||
Name: "Star Wars", | ||
Year: 1977, | ||
} | ||
starWarsIV.ID, err = moviesCollection.Create(context.Background(), starWarsIV) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Updating a movie | ||
starWarsIV.Name = "Star Wars: Episode IV - A New Hope" | ||
err = moviesCollection.UpdateID(context.Background(), starWarsIV.ID, starWarsIV) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Listing all movies | ||
allMovies, err := moviesCollection.All(context.Background()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("All movies from Mongo: ", allMovies) | ||
|
||
// Deleting a movie | ||
err = moviesCollection.DeleteID(context.Background(), starWarsIV.ID) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
} |