What happens behind Connect, Database, Collection in the Go driver?

Hello, I understant connect will build a connection with database, return the connection to client, so the “client” here is similar to the concept “session” in globalsign/mgo, right? but what happens in client.Database(“db_name”), I know it will return a reference of the database, such reference is thread safe to use? what happens in db.Collection(“coll_name”)? the reference of collection returned is thread safe too?

1 Like

Hi @Zhihong_GUO,

so the “client” here is similar to the concept “session” in globalsign/mgo, right?

Although there are similarities between them, they are not quite the same. For example, there is no methods such as session.Copy() or session.Clone(). With mongo-go-driver, you just dial one client which can be passed around between routines (each running whatever commands it needed), and the connection pooling is handled for you.

Also, worth mentioning that there’s also Session in mongo-go-driver, which is an interface that represents a MongoDB logical session.

I know it will return a reference of the database, such reference is thread safe to use? what happens in db.Collection(“coll_name”)?

Client, Database and Collection are safe for concurrent use by multiple goroutines.

Regards,
Wan.

Thank you Wan. So I can do something like that:

type MyService struct {
coll *mongo.Collection //I will save the collect as a member
}

//PaginateDocs can be accessed by several “client” apps
func (serv *MyService) PaginateDocs (id int, filter Filter, opt FindOptions, result interface{}) error {
c, err := serv.coll.Find(context.TODO(), filter, opt) //so here I can use the member, without concern of thread safe
err = c.All(context.TODO(), &result)
return err
}

Thank you for your answer.

Yes, you can pass mongo.Collection around in the application.

Regards,
Wan.

Thanks a lot for the answer!

I am sharing db.Collection(“coll_name”) in my application and have a query (I think my query is on the same line of this thread “What happens behind collection in the Go driver”)

  • Does the db.Collection("coll_name) hold the reference to the connection pool or does it hold the reference to a particular connection in the connection pool?
    e.g. If I am calling FIND method multiple times on the shared db.Collection(“coll_name”) object, will it use the same connection or any available connection from the pool?

it should only hold a reference to the connection (maybe indirectly) pool.
Connections are shared by all queries, so whichever is available, the query will try using it.

multiple queries are supposed to use different connections if one is available. (i’m not a mongo employee btw). this is so that requests don’t block each other. Just like there is normally only one active request on a single http connection. (no http pipelining).

yes, makes sense, thanks a lot for the answer

The mongo.Connect() method in the Go driver for MongoDB creates a new client to connect to a MongoDB server. When you call Connect() ,