getMore

The `getMore` command is used to retrieve extra batches of documents from an existing cursor. This command is useful when dealing with large datasets that can't be fetched in a single query due to size limitations.

The command allows clients to paginate through the results in manageable chunks with commands that return a cursor, such as `find` and `aggregate`, to return subsequent batches of documents currently pointed to by the cursor.

Syntax

{
  getMore: <cursor-id>,
  collection: <collection-name>,
  batchSize: <number-of-documents>
}

Parameters

getMorenumberrequired

The unique identifier for the cursor from which to retrieve more documents.

collectionstringrequired

The name of the collection associated with the cursor.

batchSizenumber

The number of documents to return in the batch. If not specified, the server uses the default batch size.

Examples

Sample Data

{
  "_id": "00000000-0000-0000-0000-000000001001",
  "name": "Example Store",
  "location": {
    "lat": 40.7128,
    "lon": -74.0060
  }
}

Retrieve more documents from a cursor

Retrieve the next batch of documents from a cursor with ID `1234567890`.

This command fetches the next 5 documents from the stores collection cursor.

Query:

{
  getMore: 1234567890,
  collection: "stores",
  batchSize: 5
}

Output:

{
  cursor: {
    id: 1234567890,
    ns: "StoreData.stores",
    nextBatch: [
      { _id: "...", name: "Store 1" },
      { _id: "...", name: "Store 2" },
      { _id: "...", name: "Store 3" },
      { _id: "...", name: "Store 4" },
      { _id: "...", name: "Store 5" }
    ]
  },
  ok: 1
}

Retrieve more documents without specifying batch size

If you don't specify the `batchSize`, the server uses the default batch size.

This command retrieves documents using the server's default batch size.

Query:

{
  getMore: 1234567890,
  collection: "stores"
}

Output:

{
  cursor: {
    id: 1234567890,
    ns: "StoreData.stores",
    nextBatch: [ ... ]
  },
  ok: 1
}

Related