Go lang
Programs
read csv file
find variable type in golang
for loops in golang
MongoDB sort() Method
MongoDB Find
MongoDB FindOne
differences between arrays and slices
InsertOne MongoDB
Connecting to MongoDB
how to use range in golang
Modules
Create a Go module
Create app using Go Module
Install
Installing GO lang

MongoDB FindOne on collection



MongoDB FindOne on collection

create application directory

go mod init github.com/freedomtutorials/mongodbfindone

import mongodb-drivers

go get go.mongodb.org/mongo-driver

Connect to MongoDB, with below code

    // set the mongodb uri 
    clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:27017")
	
    //connect to mongodb
    client, err := mongo.Connect(context.TODO(), clientOptions)

    if err != nil {
        log.Fatal(err)
    }

    err = client.Ping(context.TODO(), nil)

    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("Connected to MongoDB!")

    freedomtutorialsDB := client.Database("freedomtutorials")
    mydataCollection := freedomtutorialsDB.Collection("mydata")

FindOne document on collection

    var device Device
    if err = mydataCollection.FindOne( context.TODO(), bson.M{}).Decode(&device); err != nil {
        log.Fatal(err)
    }
    fmt.Println(device)

output:

{1 Name}

FindOne document on collection with filter

    if err = mydataCollection.FindOne( context.TODO(), bson.M{"id":1}).Decode(&device); err != nil {
        log.Fatal(err)
    }
    fmt.Println(device)


how to use range in golang

how to use range in golang

posted on 2022-05-03 08:28:11 - Go lang Tutorials


find variable type in golang

find variable type in golang

posted on 2022-05-03 05:24:55 - Go lang Tutorials


for loops in golang

for loops in golang

posted on 2022-05-03 04:27:40 - Go lang Tutorials


Run MongoDB On WSL Ubuntu

Run MongoDB On WSL Ubuntu

posted on 2022-04-28 05:02:09 - mongodb Tutorials


Installing MongoDB

Installing MongoDB

posted on 2022-04-28 04:16:57 - mongodb Tutorials


MySQL TRIGGERS

MySQL - Triggers

posted on 2019-11-29 21:44:07 - mysql Tutorials