Interaction with MongoDb using mgo

I have been trying to query my mongodb database but it seems to not work properly.

This is my code. I am not sure if the struct is to be used the way I am doing it right now.

package main

import (

type userInfo struct {
  username string `bson:"username"`
  pass     string    `bson:"pass"`

func connect() (session *mgo.Session) {
    connectURL := "localhost:27017"
    session, err := mgo.Dial(connectURL)
    if err != nil {
        fmt.Printf("Can't connect to mongo, go error %v\n", err)
    return session

func main() {
  session := connect()
  defer session.Close()

  result := userInfo {}
  collection := session.DB("test_db").C("userlogin")
  err := collection.Find(bson.M{"username": "super_sam"}).One(&result)
        if err != nil {
                log.Println("Not in db")

        fmt.Println("Username:%s", result.pass)

Everything seems perfect based on the tutorials that I have seen so far assuming that the ones inside the literal in the struct is supposed to match the exact usage as in the database which is the way it is stored in the db(i.e. in bson format). I have used the exact names in the collection in the db as in inside the backticks(“username” and “pass”). The username “super_sam” was inserted manually into the collection yet when queried I get the “Not in db” result.

This is what I observed when I checked manually in mongodb shell after I did an insert using golang —
err := collection.Insert(&userInfo{username: “Alex”, pass: “cool”})

show collections
{ “_id” : ObjectId(“57b9e5b4717f3b9cf03b997f”) }

As you can see it is empty other than the default ID that is generated on successful insertion.

Any help would be useful. Thanks in advance.

I have used the content inside backticks properly. The edit here seems to hide that. Ignore that. No mistake in formatting there.

Haha. Sorry for putting up so many replies and thanks for your patience but I have found the solution.

It pertains to using Uppercase first letter inside the structure as in “Username” instead of “username”. That’s quite weird as to why it’s been hardcoded that kind of usage(Seems like somebody in Google is fixated with Uppercase starting letters). Anyways I would have appreciated more concrete mention of that in the docs somewhere that it mandated to be used that way(Or did I miss it?).

*Query.One(&result) can only access “public” members of your structure ‘userinfo’ - this is not a Google fixation; it is a feature of go. Variables declared in the “main” package follow the same rules as variables in any imported package.

Hi Charles

You didn’t get what I was talking about. I know that only public members can be accessed by the function. That’s pretty common across languages. I meant the fixation to use the name of the variables with an Uppercase letter for a beginning like “Username” instead of “username”. Does this affect it’s status as public or private in golang? If so you are right and I missed your point.Thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.