Navigation

mongodb.db()

Definition

mongodb.db()

Return a database handle object that represents a database in a linked MongoDB Cluster. Call database.collection() on the database handle to retrieve a collection handle.

Note

The mongodb.db() action is available on MongoDB service clients. You can instantiate a service client with the context.services.get(serviceName) method in functions or the getServiceClient() action in a client SDK.

Usage

Example

To call the mongodb.db() action from a Function, instantiate a MongoDB service client from context then call the client’s db() method.

const mongodb = context.services.get("myMongoDBCluster");
const db = mongodb.db("myDB");

To call the mongodb.db() action from a JavaScript SDK, use the RemoteMongoClient.db() method.

const mongodb = Stitch.defaultAppClient.getServiceClient(
  RemoteMongoClient.factory,
  "myMongoDBCluster"
);
const db = mongodb.db("myDB");

To call the collection.db() action from the Java/Android SDK, use the RemoteMongoClient.getDatabase() method.

RemoteMongoClient mongodb = Stitch
  .getDefaultAppClient()
  .getServiceClient(
    RemoteMongoClient.factory,
    "myMongoDBCluster"
  );
RemoteMongoDatabase db = mongodb.getDatabase("myDB");

To call the mongodb.db() action from the Swift/iOS SDK, use the RemoteMongoClient.db() method.

mongodb: RemoteMongoClient = Stitch.defaultAppClient.serviceClient(
  fromFactory: RemoteMongoClient.factory,
  withName: "myMongoDBCluster"
)
RemoteMongoDatabase db = mongodb.db("myDB")

Parameters

The db() action is available on MongoDB service clients returned from context.services.get(serviceName) as the db method, which has the following form:

The db() action has the following form:

db(name)

The db() method has the following form:

db(name)

The getDatabase() method has the following form:

getDatabase​(String name)

The db() method has the following form:

db(name)
Parameter Description

Database Name

name: <string>
The name of the database. The database must exist in the linked cluster associated with the MongoDB service client.

Return Value

The mongodb.db() action returns a database object that allows you to access collections in the specified database.

See database.collection()

The RemoteMongoClient.db() method returns a RemoteMongoDatabase object that allows you to access collections in the specified database.

The RemoteMongoClient.getDatabase() method returns a RemoteMongoDatabase object that allows you to access collections in the specified database.

The RemoteMongoClient.db() method returns a RemoteMongoDatabase object that allows you to access collections in the specified database.