@@ -5,13 +5,13 @@ import (
5
5
"log"
6
6
"net/http"
7
7
"os"
8
- "context"
9
- "time"
10
-
8
+ // "context"
9
+ // "time"
10
+ "go.mongodb.org/mongo-driver/bson"
11
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"
12
+ // "go.mongodb.org/mongo-driver/mongo"
13
+ // "go.mongodb.org/mongo-driver/mongo/options"
14
+ // "go.mongodb.org/mongo-driver/mongo/readpref"
15
15
16
16
"github.com/joho/godotenv"
17
17
"github.com/gin-gonic/gin"
@@ -56,6 +56,37 @@ func main() {
56
56
// Ping mongoDB with Ping method
57
57
mongoconnect .Ping (client , ctx )
58
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
+
59
90
port := os .Getenv ("PORT" )
60
91
61
92
router := gin .Default ()
@@ -81,58 +112,3 @@ func main() {
81
112
func helloCall (c * gin.Context ) {
82
113
c .JSON (http .StatusOK , gin.H {"message" : "Hello, You created a Web App!" })
83
114
}
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
- }
0 commit comments