what’s the most expensive part in terms of time consumed to prepare a connection to issue a command/query against a MongoDB database collection - speaking of the Node.js driver?
Is it the MongoClient.connect() call, the client.db('dbName') or the db.collection('collectionName') part?
Welcome to the community, Alex! Glad to have you here.
Great question.
The most expensive would be the connect call like Steeve mentioned. And, the reason it’s expensive is, clients connect to the database with a regular TCP connection which requires the three-way handshake to establish a new connection. On top of that, each connection need to be authenticated and authorized, adding to the delay. In order to avoid this, most applications tend to reuse existing open connections to decrease the latency. Mongo drivers achieve this via connection pooling to help use the resources efficiently.
Now, about your other two calls. They should be less expensive assuming they will piggyback on an existing open connection. Hope this helps.
Thanks,
Mahi
P.S. I’ve oversimplified the process without accounting for optimizations in TCP and other aspects, but overall, initial setup is expensive compared to reuse.