HTML Form MongoDB InsertOne – GinGonic Golang Framework 16

HTML Form MongoDB InsertOne – GinGonic Golang Framework 16

Source Code – Main.go

<div>//Mongodb insert data via html form(gingonic)</div>
<div>// first install go get</div>
<div>package main</div>
<div>import (</div>
<div>    "context"</div>
<div>    ""</div>
<div>    ""</div>
<div>    ""</div>
<div>    ""</div>
<div>// lets create context variable</div>
<div>var ctx = context.TODO()</div>
<div>// lets create collection variable</div>
<div>var collection *mongo.Collection</div>
<div>// initialize mongodb</div>
<div>func init() {</div>
<div>    // 2nd value/parameter is url of mongodb</div>
<div>    client, _ := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))</div>
<div>    // in result it gives client &amp; err(ignored here)</div>
<div>    // lets create database and collection(table)</div>
<div>    collection = client.Database("anyname").Collection("anyname")</div>
<div>func main() {</div>
<div>    // lets create gingonic server</div>
<div>    r := gin.Default()</div>
<div>    r.Static("/insert", "./static") // we will keep our html file in this folder</div>
<div>    // lets create our routes</div>
<div>    r.GET("/", func(c *gin.Context) {</div>
<div>        // get data from form</div>
<div>        name := c.Query("name")</div>
<div>        value := c.Query("value")</div>
<div>        // lets insert data into mongodb</div>
<div>        collection.InsertOne(ctx, bson.D{{"name", name}, {"value", value}})</div>
<div>        // lets send some response to user</div>
<div>        //c.JSON(200, []string{name,value})</div>
<div>        //c.String(200, "success message....")</div>
<div>        c.String(200, "success... %s %s", name, value)</div>
<div>        // you can use any of the above 3 ...</div>
<div>    })</div>
<div>    r.Run()</div>


Source Code – index.html

<div>&lt;!DOCTYPE html&gt;</div>
<div>&lt;html lang="en"&gt;</div>
<div>    &lt;meta charset="UTF-8"&gt;</div>
<div>    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;</div>
<div>    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;</div>
<div>    &lt;title&gt;Insert data into gingonic/mongodb&lt;/title&gt;</div>
<div>    &lt;!-- Create html form --&gt;</div>
<div>    &lt;form action="/" method="get"&gt;</div>
<div>        &lt;!-- input fields --one for value and one for name --&gt;</div>
<div>        &lt;input type="text" name="name" id=""&gt;</div>
<div>        &lt;input type="text" name="value" id=""&gt;</div>
<div>        &lt;!-- I need only 2 fields ... you increse or decrease --&gt;</div>
<div>        &lt;!-- submit button --&gt;</div>
<div>        &lt;button type="submit"&gt;submit&lt;/button&gt;</div>
<div>    &lt;/form&gt;</div>



gin gonic tutorial
gin-gonic / examples
gin-gonic documentation
gin framework tutorial
gin web framework
gin router
golang gin context
gin router group

Tony BB

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: