0

I'm having the next issue.. I can't get all records from my mongo database (running in a docker containner), here is my really simple code:

type User struct {
    Email string `json:"email" bson:"email"`
    Pass  string `json:"pass" bson:"pass"`
}

session := dbConnect()
collection := session.DB("my_db").C("users")
var users []User
err := collection.Find(nil).All(&users)
if err != nil {
    log.Fatal("Mongo collection find fail: ", err)
}
fmt.Println("Results >> ", err)

The thing is "err" returns <nil>. Something weird is that inserting documents on the database actually works. Thank you!

Marsel Novy
  • 1,777
  • 16
  • 23
Gerardo Tarragona
  • 1,185
  • 4
  • 15
  • 28

1 Answers1

1

There is no issue with your code, but the last line fmt.Println("Results >> ", err), shouldn't it be fmt.Println("Results >> ", users) ?

Also you can check if collection is not empty and fields are correct by using interface

session := dbConnect()
    collection := session.DB("my_db").C("users")
    var v []interface{}
    err := collection.Find(nil).All(&v)
    if err != nil {
        log.Fatal("Mongo collection find fail: ", err)
    }
    fmt.Println("Results >> ", v)
Marsel Novy
  • 1,777
  • 16
  • 23