Skip to content

Commit 6d2e80d

Browse files
mongodb connect
1 parent eabc247 commit 6d2e80d

File tree

2 files changed

+102
-65
lines changed

2 files changed

+102
-65
lines changed

main.go

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import (
55
"log"
66
"net/http"
77
"os"
8+
89
// "context"
9-
// "time"
10-
"go.mongodb.org/mongo-driver/bson"
11-
"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"
15-
16-
"github.com/joho/godotenv"
10+
// "time"
11+
"github.com/vinodnextcoder/golang-mongo-server/mongoconnect"
12+
13+
// "go.mongodb.org/mongo-driver/mongo"
14+
// "go.mongodb.org/mongo-driver/mongo/options"
15+
// "go.mongodb.org/mongo-driver/mongo/readpref"
16+
1717
"github.com/gin-gonic/gin"
18+
"github.com/joho/godotenv"
1819
swaggerFiles "github.com/swaggo/files"
1920
ginSwagger "github.com/swaggo/gin-swagger"
2021
_ "github.com/vinodnextcoder/golang-mongo-server/docs"
21-
2222
)
2323

2424
// @title Swagger Example API
@@ -37,70 +37,65 @@ import (
3737
func main() {
3838

3939
err := godotenv.Load(".env.dev")
40-
41-
if err != nil {
42-
log.Fatal("Error loading .env file")
43-
}
44-
45-
// Get Client, Context, CancelFunc and
46-
// err from connect method.
47-
client, ctx, cancel, err := mongoconnect.Connect("mongodb://localhost:27017/test")
48-
if err != nil {
49-
panic(err)
50-
}
51-
52-
// Release resource when the main
53-
// function is returned.
54-
defer mongoconnect.Close(client, ctx, cancel)
55-
56-
// Ping mongoDB with Ping method
57-
mongoconnect.Ping(client, ctx)
58-
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-
90-
port := os.Getenv("PORT")
40+
41+
if err != nil {
42+
log.Fatal("Error loading .env file")
43+
}
44+
45+
// Get Client, Context, CancelFunc and
46+
// err from connect method.
47+
client, ctx, cancel, err := mongoconnect.Connect("mongodb://localhost:27017/test")
48+
if err != nil {
49+
panic(err)
50+
}
51+
52+
// Release resource when the main
53+
// function is returned.
54+
// defer mongoconnect.Close(client, ctx, cancel)
55+
56+
// // Ping mongoDB with Ping method
57+
// // mongoconnect.Ping(client, ctx)
58+
59+
// // Create a object of type interface to store
60+
// // the bson values, that we are inserting into database.
61+
// var document interface{}
62+
63+
// document = bson.D{
64+
// {"rollNo", 175},
65+
// {"maths", 80},
66+
// {"science", 90},
67+
// {"computer", 95},
68+
// }
69+
70+
// dbname := os.Getenv("DBNAME")
71+
// // insertOne accepts client , context, database
72+
// // name collection name and an interface that
73+
// // will be inserted into the collection.
74+
// // insertOne returns an error and a result of
75+
// // insert in a single document into the collection.
76+
// insertOneResult, err := mongoconnect.Insertdata(client, ctx, dbname, "marks", document)
77+
78+
// handle the error
79+
if err != nil {
80+
panic(err)
81+
}
82+
83+
// print the insertion id of the document,
84+
// if it is inserted.
85+
fmt.Println("Result of InsertOne", client, ctx, cancel)
86+
// fmt.Println(insertOneResult.InsertedID)
87+
88+
port := os.Getenv("PORT")
9189

9290
router := gin.Default()
9391

9492
router.GET("/", helloCall)
95-
router.POST("/add", mongoconnect.PostMarks)
93+
router.POST("/add", mongoconnect.PostMarks)
9694
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
9795

98-
99-
router.Run("0.0.0.0:"+port)
96+
router.Run("0.0.0.0:" + port)
10097
}
10198

102-
103-
10499
// helloCall godoc
105100
// @Summary hellow example
106101
// @Schemes

mongoconnect/config.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"os"
78
"time"
89

910
// "go.mongodb.org/mongo-driver/bson"
@@ -98,6 +99,25 @@ func Insertdata(client *mongo.Client, ctx context.Context, dataBase, col string,
9899
return result, err
99100
}
100101

102+
func Insertdata1(dataBase, col string, doc interface{}) (*mongo.InsertOneResult, error) {
103+
104+
// connect mongodb and insert records
105+
client, ctx, cancel, err := Connect("mongodb://localhost:27017/test")
106+
if err != nil {
107+
panic(err)
108+
}
109+
110+
// select database and collection ith Client.Database method
111+
// and Database.Collection method
112+
fmt.Println("Result of InsertOne", cancel)
113+
collection := client.Database(dataBase).Collection(col)
114+
115+
// InsertOne accept two argument of type Context
116+
// and of empty interface
117+
result, err := collection.InsertOne(ctx, doc)
118+
return result, err
119+
}
120+
101121
// for testing added
102122

103123
func PostMarks(c *gin.Context) {
@@ -111,7 +131,29 @@ func PostMarks(c *gin.Context) {
111131
// Handle error
112132
return
113133
}
134+
Connect("mongodb://localhost:27017/test")
135+
dbname := os.Getenv("DBNAME")
136+
// var client mongo.Client
137+
// var ctx context.Context
138+
// insertOne accepts client , context, database
139+
// name collection name and an interface that
140+
// will be inserted into the collection.
141+
// insertOne returns an error and a result of
142+
// insert in a single document into the collection.
143+
144+
insertOneResult, err := Insertdata1(dbname, "marks", jsonBody)
145+
146+
fmt.Println(err)
147+
148+
// handle the error
149+
if err != nil {
150+
panic(err)
151+
}
114152

153+
// print the insertion id of the document,
154+
// if it is inserted.
155+
fmt.Println("Result of InsertOne")
156+
fmt.Println(insertOneResult.InsertedID)
115157
// Use the JSON object
116158
fmt.Println(jsonBody)
117159
c.IndentedJSON(http.StatusCreated, jsonBody)

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