How to close app.currentUser.mongoClient connection

From the below documentation, I have established a connection to mongodb instance. However, the returned object does not have ‘close’ as a function. How do I close this connection?

Connect to a Linked Cluster

To access a linked cluster from your client application, pass the cluster name to User.mongoClient(). This returns a MongoDB service interface that you can use to access databases and collections in the cluster.

const mongodb = app.currentUser.mongoClient("mongodb-atlas");
const plants = mongodb.db("example").collection("plants");

From the “returns” chain, I would say it does not have a “close” method on its own and closes when you close the realm realm.close(), or garbage collected if it goes out of scope. User.logOut should also close it if it matters.

Class: User (
User.mongoClient:MongoDB → db: MongoDBDatabase → collection: Realm.MongoDBCollection → collection methods

Hi Folks – There’s no close method associated here as the request is routed through Atlas App Services vs. connecting to the database directly. Atlas App Services serves as a proxy between the client and Atlas cluster and will open/pool/close connections automatically so there is no need to explicitly close the connection. Note, that while this may lead to slightly more connections open than expected at lower levels of usage it means that overall requests made via Atlas App Services will be very efficient with connection usage.


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