https://github.com/root27/go-mongodb
Package provides mongo connection
https://github.com/root27/go-mongodb
Last synced: 23 days ago
JSON representation
Package provides mongo connection
- Host: GitHub
- URL: https://github.com/root27/go-mongodb
- Owner: root27
- License: gpl-3.0
- Created: 2023-06-15T06:09:47.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-08T09:26:18.000Z (over 2 years ago)
- Last Synced: 2025-02-27T11:47:14.574Z (over 1 year ago)
- Language: Go
- Size: 34.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Go MongoDB
---
### Description
This is a simple package to connect to a MongoDB database.
### Connection and Get Collection Usage
```go
package main
import (
"github.com/root27/go-mongodb"
)
func main(){
db ,err:= mongodb.Connect("mongourl") // mongourl is the env variable name
if err != nil {
panic(err)
}
// db is a pointer to a mongo.Database
// use it to perform operations on the database
//Get Collection
collection := db.GetCollection("databaseName","collectionName")
}
```
### Insert Usage
```go
package main
import (
"github.com/root27/go-mongodb"
)
func main(){
db ,err:= mongodb.Connect("mongourl") // mongourl is the env variable name
if err != nil {
panic(err)
}
// db is a pointer to a mongo.Database
// use it to perform operations on the database
//Get Collection
collection := db.GetCollection("databaseName","collectionName")
//Insert
err = collection.InsertOne(db, bson.M{"name": "pi", "value": 3.14159})
if err != nil {
panic(err)
}
}
```
### Find Usage
```go
package main
import (
"github.com/root27/go-mongodb"
)
func main(){
db ,err:= mongodb.Connect("mongourl") // mongourl is the env variable name
if err != nil {
panic(err)
}
// db is a pointer to a mongo.Database
// use it to perform operations on the database
//Get Collection
collection := db.GetCollection("databaseName","collectionName")
//Find
var result bson.M
err = collection.FindOne(db, bson.M{"name": "pi"}).Decode(&result)
if err != nil {
panic(err)
}
fmt.Println("pi:", result["value"])
}
```