Skip to content

Go import json file into mongodb unstructured

wordpress meta
title: 'Go Import JSON File Into MongoDB Unstructured'
date: '2021-03-24T16:03:24-05:00'
status: publish
permalink: /go-import-json-file-into-mongodb-unstructured
author: admin
excerpt: ''
type: post
id: 1724
category:
    - go
    - MongoDB
    - Terraform
tag: []
post_format: []

During a recent POC I imported a Terraform state file(JSON) into MongoDB using golang. This may not exactly fit any use case since it is simpler and use actual file(s) and not MongoDB to store a Terraform state file, but this helps record my effort as well as the unstructured nature of the JSON ie I am not defining a structure just using an interface{}.

Insert From File

```golang package main

import ( "context" "fmt" "os" "io/ioutil" "encoding/json" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" )

func main() { clientOptions := options.Client().ApplyURI("mongodb://localhost:27017") client, e := mongo.Connect(context.TODO(), clientOptions) CheckError(e) collection := client.Database("tfstatedb").Collection("states") jsonFile, err := os.Open("terraform.tfstate") if err != nil { fmt.Println(err) } defer jsonFile.Close()

byteValue, _ := ioutil.ReadAll(jsonFile)
var result map[string]interface{}
json.Unmarshal([]byte(byteValue), &result) 
_, e = collection.InsertOne(context.TODO(), result)
CheckError(e)

}

func CheckError(e error) { if e != nil { fmt.Println(e) } }


FindOne From DB
---------------

```golang
package main

import (
    "flag"
    "log"
    "context"
    "fmt"
    "encoding/json"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

var (
     flagId string
)

func init() {
     flag.StringVar(&flagId, "id", "", "mongodb ObjectId")
}

func PrettyPrint(v interface{}) (err error) {
      b, err := json.MarshalIndent(v, "", "  ")
      if err == nil {
         fmt.Println(string(b))
      }
      return
}

func main() {
    if !flag.Parsed() {
    flag.Parse()
    }
    if flagId != "" {
          fmt.Println("finding id: " + flagId)
    } else {
          log.Fatal("no _id specified")
    }

    clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
    client, e := mongo.Connect(context.TODO(), clientOptions)
    CheckError(e)

    e = client.Ping(context.TODO(), nil)
    CheckError(e)

    collection := client.Database("tfstatedb").Collection("states")

    var res interface{}

    docID, e := primitive.ObjectIDFromHex(flagId)
    CheckError(e)

    filter := bson.M{"_id": docID}

    tempResult := bson.M{}
    e = collection.FindOne(context.TODO(), filter).Decode(&tempResult)
    if e == nil {   
      obj, _ := json.Marshal(tempResult)
      e = json.Unmarshal(obj, &res)
    }
    PrettyPrint(res)
}

func CheckError(e error) {
    if e != nil {
        fmt.Println(e)
    }
}