{"id":1724,"date":"2021-03-24T16:03:24","date_gmt":"2021-03-24T21:03:24","guid":{"rendered":"https:\/\/blog.iqonda.net\/?p=1724"},"modified":"2021-03-24T16:03:24","modified_gmt":"2021-03-24T21:03:24","slug":"go-import-json-file-into-mongodb-unstructured","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/go-import-json-file-into-mongodb-unstructured\/","title":{"rendered":"Go Import JSON File Into MongoDB Unstructured"},"content":{"rendered":"

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{}. <\/p>\n

Insert From File<\/h2>\n
package main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"os\"\n    \"io\/ioutil\"\n    \"encoding\/json\"\n    \"go.mongodb.org\/mongo-driver\/mongo\"\n    \"go.mongodb.org\/mongo-driver\/mongo\/options\"\n)\n\nfunc main() {\n    clientOptions := options.Client().ApplyURI(\"mongodb:\/\/localhost:27017\")\n    client, e := mongo.Connect(context.TODO(), clientOptions)\n    CheckError(e)\n    collection := client.Database(\"tfstatedb\").Collection(\"states\")\n    jsonFile, err := os.Open(\"terraform.tfstate\")\n    if err != nil {\n        fmt.Println(err)\n    }\n    defer jsonFile.Close()\n\n    byteValue, _ := ioutil.ReadAll(jsonFile)\n    var result map[string]interface{}\n    json.Unmarshal([]byte(byteValue), &result) \n    _, e = collection.InsertOne(context.TODO(), result)\n    CheckError(e)\n}\n\nfunc CheckError(e error) {\n    if e != nil {\n        fmt.Println(e)\n    }\n}<\/code><\/pre>\n

FindOne From DB<\/h2>\n
package main\n\nimport (\n    \"flag\"\n    \"log\"\n    \"context\"\n    \"fmt\"\n    \"encoding\/json\"\n    \"go.mongodb.org\/mongo-driver\/bson\"\n    \"go.mongodb.org\/mongo-driver\/bson\/primitive\"\n    \"go.mongodb.org\/mongo-driver\/mongo\"\n    \"go.mongodb.org\/mongo-driver\/mongo\/options\"\n)\n\nvar (\n     flagId string\n)\n\nfunc init() {\n     flag.StringVar(&flagId, \"id\", \"\", \"mongodb ObjectId\")\n}\n\nfunc PrettyPrint(v interface{}) (err error) {\n      b, err := json.MarshalIndent(v, \"\", \"  \")\n      if err == nil {\n         fmt.Println(string(b))\n      }\n      return\n}\n\nfunc main() {\n    if !flag.Parsed() {\n    flag.Parse()\n    }\n    if flagId != \"\" {\n          fmt.Println(\"finding id: \" + flagId)\n    } else {\n          log.Fatal(\"no _id specified\")\n    }\n\n    clientOptions := options.Client().ApplyURI(\"mongodb:\/\/localhost:27017\")\n    client, e := mongo.Connect(context.TODO(), clientOptions)\n    CheckError(e)\n\n    e = client.Ping(context.TODO(), nil)\n    CheckError(e)\n\n    collection := client.Database(\"tfstatedb\").Collection(\"states\")\n\n    var res interface{}\n\n    docID, e := primitive.ObjectIDFromHex(flagId)\n    CheckError(e)\n\n    filter := bson.M{\"_id\": docID}\n\n    tempResult := bson.M{}\n    e = collection.FindOne(context.TODO(), filter).Decode(&tempResult)\n    if e == nil {   \n      obj, _ := json.Marshal(tempResult)\n      e = json.Unmarshal(obj, &res)\n    }\n    PrettyPrint(res)\n}\n\nfunc CheckError(e error) {\n    if e != nil {\n        fmt.Println(e)\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

During a recent POC I imported a Terraform state file(JSON) into MongoDB using golang. This may not exactly fit any<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,125,88],"tags":[],"class_list":["post-1724","post","type-post","status-publish","format-standard","hentry","category-go","category-mongodb","category-terraform"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1724"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1724\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}