Function Context Object
Parent: MongoDB Atlas Triggers and Functions · Published reference · snapshot 2026-10-23 · skill mongodb-cohort-f6ac572aba6f
↓ Facts as markdownall context files
Source-anchored research for Function Context Object. Run status: COMPLETED.
These notes link each claim to its source. Some citations identify archived research reports that are no longer hosted on this site. A published reference means the content is available; it does not certify independent review or accuracy.Read the editorial policy and follow the sources before relying on a claim.
How it works
- context.services.get() returns undefined when the named service does not exist; mongodb-atlas is the default linked-cluster name. (low confidence) [source]
- Database and scheduled trigger Functions execute as the system user, which bypasses application rules and permissions. (high confidence) [source]
- When consuming a called Function's result, await context.functions.execute(...) inside an async Function; a support thread illustrates this correction. (low confidence) [source]
- The documented context.http client makes outbound requests asynchronously. Its response body is BSON.Binary; response.body.text() decodes it before JSON.parse or EJSON.parse handles JSON. Parse only when the response payload is actually JSON. (high confidence) [source]
- context.services.get returns undefined when the named service does not exist. context.values.get(valueName) likewise returns undefined when the named Value does not exist. (low confidence) [source]
Problems, failure modes and limitations
- A November 2020 firsthand report describes code working under a selected editor user but failing under trigger execution with "FunctionError: cannot compare to undefined" after accessing context.user.data.email. The report has no accepted resolution. (low confidence) [source]
Comparisons and alternatives
- context.request contains request metadata rather than the request body. Header values are arrays of strings; optional metadata can be undefined. Atlas removes a query parameter whose key is secret from rawQueryString. (low confidence) [source]
- Database and Scheduled Trigger Functions execute as the system user. The system user bypasses application rules, and its user.data object is empty. Use context.runningAsSystem() to test the execution identity rather than assuming application-user profile fields are present. (high confidence) [source]
Facts and statements
- Atlas Functions receive a global context object that exposes execution metadata and application capabilities, including app, environment, functions, http, request, services, user, and values. It is supplied by the Functions runtime. (high confidence) [source]
- context.services.get(serviceName) resolves a configured linked data source by its name; mongodb-atlas is the default cluster service name. Chain db(databaseName).collection(collectionName) to obtain a collection handle. This is a runtime-provided client, not a MongoDB connection URI. (high confidence) [source]
- context.values.get(name) reads a named application Value; a Value may expose a Secret, and a missing name returns undefined. (low confidence) [source]
- A firsthand Stitch integration uses context.values.get("clarifai_key") to retrieve an API credential, demonstrating named configuration access in application code. (low confidence) [source]
- context.environment exposes the current environment tag and environment-specific values; context.app exposes application metadata. (low confidence) [source]
- In system Functions, context.user.data is empty; context.runningAsSystem() tests system execution. (low confidence) [source]
- context.functions.execute(functionName, ...args) calls a named Function with separate positional arguments and returns its result. MongoDB documents that a nested Function runs in the same context as its caller. (high confidence) [source]
- A 2022 firsthand report says a Function called through context.functions.execute retained its caller identity despite a dynamic authentication script returning runAsSystemUser; a 2024 reply reports the same symptom. This historical report does not establish a current product defect or validate that configuration key. (low confidence) [source]
- context.values.get reads named static JSON configuration or a Secret exposed through a Value. context.environment.tag and context.environment.values provide the current environment label and environment-specific configuration. (low confidence) [source]
- Query Alert's January 2026 example combines context.services collection access, context.values.get for a notification URL, and context.http.post with encodeBodyAsJSON. It demonstrates how one Function can use the context object for both database work and external notifications. (medium confidence) [source]
- The linked collection API has its own documented cursor contract: find() returns a cursor, while cursor.toArray() returns a Promise resolving to an array. Obtaining a collection through context.services does not itself materialize query results. (medium confidence) [source]
Children
- Context execution identity (frontier)
- Linked data source clients (frontier)
- Values and environment configuration (frontier)
- HTTP response decoding (frontier)
Frontier under this node: Context execution identity, HTTP response decoding, Linked data source clients, Values and environment configuration