Skip to content

Latest commit

 

History

History
92 lines (76 loc) · 1.67 KB

project.md

File metadata and controls

92 lines (76 loc) · 1.67 KB

$project

Import

const $project = require("mongo-to-sql").$project;

or

const $project = require("mongo-to-sql/stages/project");

Syntax

$project(stage, tableName, options);

Options

  • headless
    • Boolean
    • Defaults to false
    • When specified as true, it will remove the FROM keyword and the assosiated table name from the generated query.
  • tableCounter
    • Integer
    • When not specified or specified as 0, it will cause the table name to be escaped.
    • If it is greater than 0, the table name will not be escaped as it is assumed to be a subquery.

Similarities to MongoDB

  • If you exclude fields, you cannot also specify the inclusion of fields.
  • Fields with expressions will automatically have their expressions converted to their appropriate forms.

Differences with MongoDB

  • The _id field is not included by default.

Supported operators


  • $concat
  • $toLower
  • $toUpper

Field Inclusion

$project({
    "field1": 1,
    "field2": 1
}, "tableName");

Field Exclusion

$project({
    "field1": 0,
    "field2": 0
}, "tableName");

$concat

$project({
    "concatenatedFieldName": {
        "$concat": [ "$field1", " - ", "$field2" ]
    }
}, "tableName");

$toLower

$project({
    "lowercaseFieldName": {
        "$toLower": "$field1"
    },
    "lowercaseFieldName2": {
        "$toLower": "HELLO WORLD"
    }
}, "tableName");

$toUpper

$project({
    "uppercaseFieldName": {
        "$toUpper": "$field1"
    },
    "uppercaseFieldName2": {
        "$toUpper": "hello world"
    }
}, "tableName");