Skip to content

Commit b6552a7

Browse files
db golang added
1 parent 4295edb commit b6552a7

File tree

3 files changed

+55
-62
lines changed

3 files changed

+55
-62
lines changed

.env.dev

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
S3_BUCKET=YOURS3BUCKET
22
SECRET_KEY=YOURSECRETKEYGOESHERE
3-
PORT=3001
3+
PORT=3001
4+
DBNAME=test

main.go

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"log"
66
"net/http"
77
"os"
8-
"context"
9-
"time"
10-
8+
// "context"
9+
// "time"
10+
"go.mongodb.org/mongo-driver/bson"
1111
"github.com/vinodnextcoder/golang-mongo-server/mongoconnect"
12-
"go.mongodb.org/mongo-driver/mongo"
13-
"go.mongodb.org/mongo-driver/mongo/options"
14-
"go.mongodb.org/mongo-driver/mongo/readpref"
12+
// "go.mongodb.org/mongo-driver/mongo"
13+
// "go.mongodb.org/mongo-driver/mongo/options"
14+
// "go.mongodb.org/mongo-driver/mongo/readpref"
1515

1616
"github.com/joho/godotenv"
1717
"github.com/gin-gonic/gin"
@@ -56,6 +56,37 @@ func main() {
5656
// Ping mongoDB with Ping method
5757
mongoconnect.Ping(client, ctx)
5858

59+
60+
// Create a object of type interface to store
61+
// the bson values, that we are inserting into database.
62+
var document interface{}
63+
64+
65+
document = bson.D{
66+
{"rollNo", 175},
67+
{"maths", 80},
68+
{"science", 90},
69+
{"computer", 95},
70+
}
71+
72+
dbname := os.Getenv("DBNAME")
73+
// insertOne accepts client , context, database
74+
// name collection name and an interface that
75+
// will be inserted into the collection.
76+
// insertOne returns an error and a result of
77+
// insert in a single document into the collection.
78+
insertOneResult, err := mongoconnect.Insertdata(client, ctx, dbname, "marks", document)
79+
80+
// handle the error
81+
if err != nil {
82+
panic(err)
83+
}
84+
85+
// print the insertion id of the document,
86+
// if it is inserted.
87+
fmt.Println("Result of InsertOne")
88+
fmt.Println(insertOneResult.InsertedID)
89+
5990
port := os.Getenv("PORT")
6091

6192
router := gin.Default()
@@ -81,58 +112,3 @@ func main() {
81112
func helloCall(c *gin.Context) {
82113
c.JSON(http.StatusOK, gin.H{"message": "Hello, You created a Web App!"})
83114
}
84-
// This is a user defined method to close resources.
85-
// This method closes mongoDB connection and cancel context.
86-
func close(client *mongo.Client, ctx context.Context,
87-
cancel context.CancelFunc){
88-
89-
// CancelFunc to cancel to context
90-
defer cancel()
91-
92-
// client provides a method to close
93-
// a mongoDB connection.
94-
defer func(){
95-
96-
// client.Disconnect method also has deadline.
97-
// returns error if any,
98-
if err := client.Disconnect(ctx); err != nil{
99-
panic(err)
100-
}
101-
}()
102-
}
103-
104-
// This is a user defined method that returns mongo.Client,
105-
// context.Context, context.CancelFunc and error.
106-
// mongo.Client will be used for further database operation.
107-
// context.Context will be used set deadlines for process.
108-
// context.CancelFunc will be used to cancel context and
109-
// resource associated with it.
110-
111-
func connect(uri string)(*mongo.Client, context.Context,
112-
context.CancelFunc, error) {
113-
114-
// ctx will be used to set deadline for process, here
115-
// deadline will of 30 seconds.
116-
ctx, cancel := context.WithTimeout(context.Background(),
117-
30 * time.Second)
118-
119-
// mongo.Connect return mongo.Client method
120-
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
121-
return client, ctx, cancel, err
122-
}
123-
124-
// This is a user defined method that accepts
125-
// mongo.Client and context.Context
126-
// This method used to ping the mongoDB, return error if any.
127-
func ping(client *mongo.Client, ctx context.Context) error{
128-
129-
// mongo.Client has Ping to ping mongoDB, deadline of
130-
// the Ping method will be determined by cxt
131-
// Ping method return error if any occurred, then
132-
// the error can be handled.
133-
if err := client.Ping(ctx, readpref.Primary()); err != nil {
134-
return err
135-
}
136-
fmt.Println("connected successfully")
137-
return nil
138-
}

mongoconnect/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"context"
66
"time"
7+
// "go.mongodb.org/mongo-driver/bson"
78
"go.mongodb.org/mongo-driver/mongo"
89
"go.mongodb.org/mongo-driver/mongo/options"
910
"go.mongodb.org/mongo-driver/mongo/readpref"
@@ -65,3 +66,18 @@ if err := client.Ping(ctx, readpref.Primary()); err != nil {
6566
fmt.Println("connected successfully")
6667
return nil
6768
}
69+
70+
// insertOne is a user defined method, used to insert
71+
// documents into collection returns result of InsertOne
72+
// and error if any.
73+
func Insertdata(client *mongo.Client, ctx context.Context, dataBase, col string, doc interface{}) (*mongo.InsertOneResult, error) {
74+
75+
// select database and collection ith Client.Database method
76+
// and Database.Collection method
77+
collection := client.Database(dataBase).Collection(col)
78+
79+
// InsertOne accept two argument of type Context
80+
// and of empty interface
81+
result, err := collection.InsertOne(ctx, doc)
82+
return result, err
83+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy