$sql processes an SQL query of the data in a collection. The
$sql stage:
Must be the first stage in the pipeline.
Supports
SELECTandUNIONstatements only.
Use this stage for read-only queries.
Syntax
{ $sql: { statement: "<SQL-statement>", excludeNamespaces: true | false } }
Fields
Field | Type | Description | Necessity |
|---|---|---|---|
| string | SQL query or command to run. | Required |
| boolean | Flag that specifies whether to exclude the namespaces in the result set. | Optional |
Example
The following example shows the $sql syntax for querying
a sampleDB.egData collection:
{ $sql: { statement: "select * from egData limit 2", excludeNamespaces: true } }