Options
All
  • Public
  • Public/Protected
  • All
Menu

RemoteMongoCollection

The RemoteMongoCollection is the interface to a MongoDB database's collection via Stitch, allowing read and write.

It is retrieved from a RemoteMongoDatabase.

The read operations are find, count and aggregate.

The write operations are insertOne, insertMany, updateOne, updateMany, deleteOne, and deleteMany.

If you are already familiar with MongoDB drivers, it is important to understand that the RemoteMongoCollection only provides access to the operations available in Stitch. For a list of unsupported aggregation stages, see Unsupported Aggregation Stages.

Note

Log in first

A user will need to be logged in (at least anonymously) before you can read from or write to the collection. See StitchAuth.

Example

// Get the existing Stitch client.
const stitchClient = Stitch.defaultAppClient

// Get a client of the Remote Mongo Service for database access
const mongoClient = stitchClient.getServiceClient(RemoteMongoClient.factory, 'mongodb-atlas')

// 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))

See Also

Type parameters

  • DocumentT

Hierarchy

  • RemoteMongoCollection

Index

Properties

namespace

namespace: string

Gets the namespace of this collection.

Methods

aggregate

count

deleteMany

  • deleteMany(query: object): Promise<RemoteDeleteResult>
  • Removes all documents from the collection that match the given query filter. If no documents match, the collection is not modified.

    Parameters

    • query: object

      the query filter to apply the the delete operation

    Returns Promise<RemoteDeleteResult>

    a Promise containing the result of the remove many operation

deleteOne

  • deleteOne(query: object): Promise<RemoteDeleteResult>
  • Removes at most one document from the collection that matches the given filter. If no documents match, the collection is not modified.

    Parameters

    • query: object

      the query filter to apply the the delete operation

    Returns Promise<RemoteDeleteResult>

    a Promise containing the result of the remove one operation

find

  • Finds all documents in the collection that match the given query.

    An empty query ({}) will match all documents.

    Example

    // Assumption: Stitch is already logged in.
    
    // Get the existing Stitch client.
    const stitchClient = Stitch.defaultAppClient
    
    // Get a client of the Remote Mongo Service for database access
    const mongoClient = stitchClient.getServiceClient(RemoteMongoClient.factory, 'mongodb-atlas')
    
    // 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))
    

    Parameters

    • Optional query: undefined | object

      the query filter

    • Optional options: RemoteFindOptions

    Returns RemoteMongoReadOperation<DocumentT>

    a read operation which can be used to execute the query

findOne

  • findOne(query?: undefined | object, options?: RemoteFindOptions): Promise<DocumentT | null>

findOneAndDelete

  • Finds one document in the collection that matches the given query and deletes that document. (An empty query {} will match all documents)

    Parameters

    • query: object

      A Document that should match the query.

    • Optional options: RemoteFindOneAndModifyOptions

      Optional: RemoteFindOneAndModifyOptions to use when executing the command.

    Returns Promise<DocumentT | null>

    The DocumentT being deleted or null if the query returned zero matches.

findOneAndReplace

  • Finds one document in the collection that matches the given query and replaces that document with the given replacement. (An empty query {} will match all documents)

    Parameters

    • query: object

      A Document that should match the query.

    • replacement: object

      A Document that will replace the matched document

    • Optional options: RemoteFindOneAndModifyOptions

      Optional: RemoteFindOneAndModifyOptions to use when executing the command.

    Returns Promise<DocumentT | null>

    A resulting DocumentT or null if the query returned zero matches.

findOneAndUpdate

  • Finds one document in the collection that matches the given query and performs the given update on that document. (An empty query {} will match all documents)

    Parameters

    • query: object

      A Document that should match the query.

    • update: object

      A Document describing the update.

    • Optional options: RemoteFindOneAndModifyOptions

      Optional: RemoteFindOneAndModifyOptions to use when executing the command.

    Returns Promise<DocumentT | null>

    A resulting DocumentT or null if the query returned zero matches.

insertMany

  • insertMany(documents: DocumentT[]): Promise<RemoteInsertManyResult>

insertOne

  • insertOne(document: DocumentT): Promise<RemoteInsertOneResult>
  • Inserts the provided document. If the document is missing an identifier, the client should generate one.

    Parameters

    • document: DocumentT

      the document to insert

    Returns Promise<RemoteInsertOneResult>

    a Promise containing the result of the insert one operation

updateMany

  • updateMany(query: object, update: object, updateOptions?: RemoteUpdateOptions): Promise<RemoteUpdateResult>
  • Update all documents in the collection according to the specified arguments.

    Parameters

    • query: object

      a document describing the query filter, which may not be null.

    • update: object

      a document describing the update, which may not be null. The update to apply must include only update operators.

    • Optional updateOptions: RemoteUpdateOptions

      the options to apply to the update operation

    Returns Promise<RemoteUpdateResult>

    a Promise containing the result of the update many operation

updateOne

  • updateOne(query: object, update: object, updateOptions?: RemoteUpdateOptions): Promise<RemoteUpdateResult>
  • Update a single document in the collection according to the specified arguments.

    Parameters

    • query: object

      a document describing the query filter, which may not be null.

    • update: object

      a document describing the update, which may not be null. The update to apply must include only update operators.

    • Optional updateOptions: RemoteUpdateOptions

      the options to apply to the update operation

    Returns Promise<RemoteUpdateResult>

    a Promise containing the result of the update one operation

watch

  • watch(arg?: any[] | object | undefined): Promise<Stream<ChangeEvent<DocumentT>>>
  • Opens a MongoDB change stream against the collection to watch for changes. You can watch a subset of the documents in the collection by passing an array of specific document ids or a match expression that filters the ChangeEvents from the change stream.

    Defining the match expression to filter ChangeEvents is similar to defining the match expression for triggers.

    Example

    // Assumption: Stitch is already logged in and the app allows this
    // user to read/write data to a 'comments' collection.
    
    // Get the existing Stitch client.
    const stitchClient = Stitch.defaultAppClient
    
    // Get a client of the Remote Mongo Service for database access
    const mongoClient = stitchClient.getServiceClient(RemoteMongoClient.factory, 'mongodb-atlas')
    
    // Retrieve a database object
    const db = mongoClient.db('sandbox')
    
    // Retrieve the collection in the database
    const comments = db.collection('comments')
    
    // Insert a document and get its id.
    const _id = (await comments.insertOne({
      owner_id: user.id,
      text: 'Hello!',
    })).insertedId
    
    // Watch the document that was just created.
    const changeStream = await comments.watch([
      _id,
    ])
    
    // Set the change listener. This will be called
    // when the watched documents are updated.
    changeStream.onNext((event) => {
      console.log('Watched document changed:', event)
    
      // Be sure to close the stream when finished.
      changeStream.close()
    })
    
    // Update the watched document.
    await comments.updateOne({
      _id,
    }, {
      $set: {text: 'Goodbye!'},
    })
    

    Parameters

    • Optional arg: any[] | object | undefined

      Optional. An array of ids to watch or a $match expression filter. Omit to watch the entire collection.

    Returns Promise<Stream<ChangeEvent<DocumentT>>>

    a Promise containing a stream of change events representing the changes to the watched documents.

watchCompact

  • watchCompact(ids: any[]): Promise<Stream<CompactChangeEvent<DocumentT>>>
  • Opens a MongoDB change stream against the collection to watch for changes made to specific documents. The documents to watch must be explicitly specified by their _id.

    Requests a stream where the full document of update events, and several other unnecessary fields are omitted from the change event objects returned by the server. This can save on network usage when watching large documents.

    Note

    This method does not support opening change streams on an entire collection or a specific query.

    Parameters

    • ids: any[]

      the _ids of the documents to watch in this change stream

    Returns Promise<Stream<CompactChangeEvent<DocumentT>>>

    a Promise containing a stream of compact change events representing the changes to the watched documents.

withCollectionType

  • Create a new RemoteMongoCollection instance with a different default class to cast any documents returned from the database into.

    Type parameters

    • U

    Parameters

    • codec: Codec<U>

      the default class to cast any documents returned from the database into.

    Returns RemoteMongoCollection<U>

    a new CoreRemoteMongoCollection instance with the different default class

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc