Skip to content

09. Golang with mongoDB

Tanveer Alam edited this page Aug 23, 2019 · 1 revision

Golang instructions to run mongodb golang codebase


  1. Check mongod server status (Should be running)
root@freebsd:~/performance/mongodb # service mongod status
mongod is running as pid 1945

  1. Copy golang code base shared by name go_mongodb
root@freebsd:~/performance/mongodb # ll
drwxr-xr-x  3 root  wheel  512 Nov 29 03:28 go_mongodb/

root@freebsd:~/performance/mongodb # cd go_mongodb/

  1. Go get the mongodb go driver
root@freebsd:~/performance/mongodb/go_mongodb # go get "github.com/mongodb/mongo-go-driver/mongo"

  1. Compile, build and run

Note :- Uuid and timestamp gets generated in code, only input required is a file path.

root@freebsd:~/performance/mongodb/go_mongodb # go build .

root@freebsd:~/performance/mongodb/go_mongodb # ./go_mongodb 
Enter absolute path of file(utf-8 format): 
/root/performance/mongodb/go_mongodb/sample_input.csv
result = &mongo.InsertOneResult{InsertedID:objectid.ObjectID{0x5b, 0xff, 0x5f, 0x2e, 0x17, 0x7, 0x8b, 0x83, 0x87, 0xd6, 0xec, 0xbc}}

  1. Verify data by checking in mongodb shell

Note :- Database name is testdb and collection name is test_collection


root@:~/performance/datomic/datomic-free-0.9.5703 # mongo
MongoDB shell version v3.6.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.6
 
> 
> use testdb;
switched to db testdb
> 
> db.test_collection.find( {} ).pretty()
{
	"_id" : ObjectId("5bfe99f7c57cb02700c374fd"),
	"uuid" : BinData(0,"MDEwMjk1MGYtMWM1MC00ODY5LTk0ZmMtYmRhZjUzY2Y2YzI5Cg=="),
	"timestamp" : ISODate("2018-11-28T13:36:55.079Z"),
	"blob_data" : BinData(0,"Y29sMSwgY29sMiwgY29sMwp2YWwxLCB2YWwyLCB2YWwz")
}
> 
Clone this wiki locally