DEV Community

Yaşar İÇLİ
Yaşar İÇLİ

Posted on

2 3

Make the collection find using Go bongo.

For example, we have a collection of users and we want to find all users from this collection.

If you don't have bongo installed, you can install it as follows.

go get github.com/go-bongo/bongo
Enter fullscreen mode Exit fullscreen mode
package main

import (
  "log"

  "github.com/globalsign/mgo/bson"
  "github.com/go-bongo/bongo"
)

type User struct {
  bongo.DocumentBase `bson:",inline"`
  Username          string
}

func main() {

  // Bongo Connection Config
  config := &bongo.Config{
    ConnectionString: "localhost",
    Database:         "test",
  }

  connection, err := bongo.Connect(config)

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

  // Let's see how we can get the user list using bongo.

  user := &User{}
  users := []User{}

  find := connection.Collection("users").Find(bson.M{})

  for find.Next(user) {
    users = append(users, *user)
  }

  log.Println(users)

  /*
    2020/01/24 11:12:51 [{{ObjectIdHex("5e29cd071c291d4f567c482c") 2020-01-23 16:42:47.276 +0000 UTC 2020-01-23 16:42:47.276 +0000 UTC true} Testy McGee male} {{ObjectIdHex("5e2a9e84e30560746c0459c9") 2020-01-24 07:36:36.02 +0000 UTC 2020-01-24 07:36:36.02 +0000 UTC true} yasiari İÇLİ ERKEK}]
  */
}
Enter fullscreen mode Exit fullscreen mode

It's simple and short.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay