Docs Menu
Docs Home
/
MongoDB Atlas
/ / /

$sql

On this page

  • Syntax
  • Fields
  • Example

$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
}
}

Back

Retrieve Query History