Golang で MongoDB のデータを作成 (Create)

Golang で MongoDB のデータを作成します。

ライブラリーのインストール

```bash
go get gopkg.in/mgo.v2
go get github.com/joho/godotenv
```

```golang:mongo_create.go
// ---------------------------------------------------------------
//
//	mongo_create.go
//
//					Jun/11/2019
// ---------------------------------------------------------------
package main

import (
	"fmt"
	"os"
	mgo "gopkg.in/mgo.v2"
	"time"
	"github.com/joho/godotenv"
)

type City struct {
	Key	string
	Name	string
	Population	int
	Date_mod	time.Time
}

var (
	IsDrop = true
)

// ---------------------------------------------------------------
func main() {
	fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
	
	err := godotenv.Load(".env")
	if err != nil {
		panic(err)
	}


	user := os.Getenv("user")
	password := os.Getenv("password")

	mongoDBDialInfo := &mgo.DialInfo{
	Addrs:    []string{"127.0.0.1"},
	Timeout:  60 * time.Second,
	Username: user,
	Password: password,
}

	session, err := mgo.DialWithInfo(mongoDBDialInfo)
	if err != nil {
		panic(err)
	}

	defer session.Close()

	session.SetMode(mgo.Monotonic, true)

	db_name := "city"
	if IsDrop {
		err = session.DB(db_name).DropDatabase()
		if err != nil {
			panic(err)
		}
	}


	cc := session.DB(db_name).C("saitama")

	dict_aa := data_prepare_proc ()
	for key,value := range dict_aa {
		name := value["name"].(string)
		population := value["population"].(int)
		date_mod := value["date_mod"].(string)
		tt,_ := time.Parse("2006-1-2",date_mod)
		err = cc.Insert(
		&City{Key: key,Name: name, Population: population, Date_mod: tt})
		if err != nil {
			panic(err)
		}
	}

	fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ---------------------------------------------------------------
func data_prepare_proc () map[string](map[string]interface{}) {
	dict_aa := make (map[string](map[string]interface{}))

	dict_aa["t1161"] = unit_gen_proc ("さいたま",91854,"1921-5-19")
	dict_aa["t1162"] = unit_gen_proc ("所沢",36578,"1921-6-20")
	dict_aa["t1163"] = unit_gen_proc ("越谷",18394,"1921-4-14")
	dict_aa["t1164"] = unit_gen_proc ("久喜",76512,"1921-7-31")
	dict_aa["t1165"] = unit_gen_proc ("熊谷",61459,"1921-3-28")
	dict_aa["t1166"] = unit_gen_proc ("秩父",85623,"1921-1-10")
	dict_aa["t1167"] = unit_gen_proc ("川越",15947,"1921-4-19")
	dict_aa["t1168"] = unit_gen_proc ("和光",68231,"1921-7-28")
	dict_aa["t1169"] = unit_gen_proc ("新座",71948,"1921-6-8")

	return (dict_aa)
}

// ---------------------------------------------------------------
func unit_gen_proc (name string,population int,date_mod string) map[string]interface{} {
	unit_aa := make (map[string]interface{})
	unit_aa["name"] = name
	unit_aa["population"] = population
	unit_aa["date_mod"] = date_mod

	return (unit_aa)
}
// ---------------------------------------------------------------
```

```text:Makefile
mongo_create: mongo_create.go
	go build mongo_create.go
clean:
	rm -f mongo_create
```

```text:.env
user = '******'
password = '******'
```

実行コマンド

```bash
./mongo_create
```

次のバージョンで確認しました。

```text
$ go version
go version go1.13.8 linux/amd64

$ mongo --version
MongoDB shell version v3.6.8
git version: 8e540c0b6db93ce994cc548f000900bdc740f80a
OpenSSL version: OpenSSL 1.1.1f  31 Mar 2020
allocator: tcmalloc
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64
```