Once the RemoteMongoClient is instantiated, you can use the db method to access
a RemoteMongoDatabase. A RemoteMongoDatabase will then provide access to
a RemoteMongoCollection, where you can read and write data.
Note: The client needs to log in (at least anonymously) to use the database.
See StitchAuth.
const stitchAppClient = Stitch.defaultAppClient
// Get the RemoteMongoClient via the service client interfaceconst mongoClient = stitchAppClient.getServiceClient(RemoteMongoClient.factory, 'mongodb-atlas')
// Log in to access the database. Anonymous Authentication is enabled from the Stitch UI.
stitchAppClient.auth
.loginWithCredential(new AnonymousCredential())
.then(() => {
// Retrieve a database objectconst db = mongoClient.db('video')
// Retrieve the collection in the databaseconst movieDetails = db.collection('movieDetails')
// Find 10 documents and log them to console.
movieDetails.find({}, {limit: 10})
.toArray()
.then(results =>console.log('Results:', results))
.catch(console.error)
})
.catch(console.error)
RemoteMongoClient
The RemoteMongoClient can be used to get database and collection objects for interacting with MongoDB data via the Stitch MongoDB service.
Service clients are created with StitchAppClient.getServiceClient by passing RemoteMongoClient.factory and the "Stitch Service Name" found under Clusters in the Stitch control panel ("mongodb-atlas" by default).
Once the RemoteMongoClient is instantiated, you can use the db method to access a RemoteMongoDatabase. A RemoteMongoDatabase will then provide access to a RemoteMongoCollection, where you can read and write data.
Note: The client needs to log in (at least anonymously) to use the database. See StitchAuth.
Example
const stitchAppClient = Stitch.defaultAppClient // Get the RemoteMongoClient via the service client interface const mongoClient = stitchAppClient.getServiceClient(RemoteMongoClient.factory, 'mongodb-atlas') // Log in to access the database. Anonymous Authentication is enabled from the Stitch UI. stitchAppClient.auth .loginWithCredential(new AnonymousCredential()) .then(() => { // Retrieve a database object const db = mongoClient.db('video') // Retrieve the collection in the database const movieDetails = db.collection('movieDetails') // Find 10 documents and log them to console. movieDetails.find({}, {limit: 10}) .toArray() .then(results => console.log('Results:', results)) .catch(console.error) }) .catch(console.error)
See Also