DEV Community


Posted on

nodejs vs golang with MongoDB

I'm trying to learn and use MongoDB, so i had try to connect a app in golang and an other in nodejs, below the two piece of code of both languages...
They are very similar, probably in nodejs is a bit simple, but in both case MongoDB offer a very good documentations.


As packages I had used the offical mongodb packages (import "").

go get
Enter fullscreen mode Exit fullscreen mode

in my main.go

package main

import (


type agenda struct {
    Nome    string
    Cognome string
    Nazione string

func main() {
    client, err := mongo.NewClient("mongodb://")
    if err != nil {
    err = client.Connect(context.TODO())
    if err != nil {

    collection := client.Database("demo").Collection("agenda")
    cur, err := collection.Find(context.Background(), nil)
    if err != nil {
    defer cur.Close(context.Background())
    fmt.Println("test", cur)

    for cur.Next(context.Background()) {
        elem := bson.NewDocument()
        if err = cur.Decode(elem); err != nil {
            fmt.Errorf("readTasks: couldn't make to-do item ready for display: %v", err)
Enter fullscreen mode Exit fullscreen mode

Run application:

go run main.go
Enter fullscreen mode Exit fullscreen mode


Also in nodejs I had used official mongodb library:

In your app folder:

npm install mongodb --save
Enter fullscreen mode Exit fullscreen mode

my package.json file

  "name": "nodejs-mongodb",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongodb": "^3.1.8"
Enter fullscreen mode Exit fullscreen mode

my app.js file:

// mongodb
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// connection to mongodb url
const mongohost = 'mongodb://';

// create a new MongoClient
const client = new MongoClient(mongohost);

// database name
const dbName = 'demo';

// connect to the server
client.connect(function(err) {
    assert.equal(null, err);
    console.log("Connected succesfully to server db");

    // passo db
    const db = client.db(dbName);
    const collection = db.collection('agenda');
    collection.find({}).toArray(function(err, docs){
        assert.equal(err, null);
        console.log("trovati i seguenti record");
        for (i = 0; i < docs.length; i++) {
            console.log(docs[i].nome + " " + docs[i].cognome + " " + docs[i].nazione);
Enter fullscreen mode Exit fullscreen mode

Launch app:

node app.js
Enter fullscreen mode Exit fullscreen mode

Have a nice day!




Discussion (3)

mendoza profile image
David Mendoza (He/Him)

Great post man <3 !
I just have a question, how would you go about deploying a golang server?
Something like pm2? or how do you do it?

mossnana profile image
Permpoon Chaowanaphunphon

I recommended take your golang app to execute with k8s and ingress

pierangelo1982 profile image
Pierangelo Author

i think that this is a good solutions: