Docs Menu

Docs HomeMongoDB Atlas

$sql

On this page

  • Syntax
  • Fields
  • Example

Note

Beta

The support for SQL format queries is available as a Beta feature. The feature and the corresponding documentation may change at any time during the Beta stage.

$sql processes an SQL query of the data in a collection. The $sql stage:

  • Must be the first stage in the pipeline.

  • Supports SELECT and UNION statements only.

Use this stage for read-only queries.

{
$sql: {
statement: "<SQL-statement>",
excludeNamespaces: true | false
}
}
Field
Type
Description
Necessity
statement
string
SQL query or command to run.
Required
excludeNamespaces
boolean
Flag that specifies whether to exclude the namespaces in the result set.
Optional

The following example shows the $sql syntax for querying a sampleDB.egData collection:

{
$sql: {
statement: "select * from egData limit 2",
excludeNamespaces: true
}
}
←  Retrieve Federated Database Instance Query HistoryData Federation Limitations →