dotConnect for MongoDB Documentation
On this page
    Collections as Tables
    On this page

    In SQL mode, dotConnect for MongoDB represents MongoDB collections as tables and document fields as columns. This topic describes how collections and documents are mapped to tables and columns.

    Collections

    Columns

    dotConnect for MongoDB determines the columns of a table and their types by analyzing the first documents of the collection. The number of documents to analyze is set by the Describe Amount connection string parameter (100 by default).

    If a field has values of different types in the analyzed documents, the column type is determined in the following way:

    A table can have up to 2000 columns.

    Nested Fields

    Fields of embedded documents and array elements are represented as columns with dotted names, for example, address.city or tags.0.

    In SQL statements, enclose such column names in double quotes or square brackets, for example, "address.city" or [address.city]. Otherwise, the statement fails with the "no such column: address.city" error.

    Special BSON Types

    The following table lists the special BSON types that are split into several columns.

    BSON type Columns
    Regular expression re.pattern, re.options
    Binary data bin.base64, bin.subType
    Timestamp ts.t, ts.i
    JavaScript code code.$code

    In this table, re, bin, ts, and code are field names.

    Embedded Documents and Arrays as JSON

    You can represent embedded documents and arrays as JSON columns instead of dotted columns:

    When you write data to such a column, a text value that starts with { or [ is parsed as Extended JSON.

    The fields of such a JSON document are also available as hidden columns. Hidden columns aren't returned by SELECT *, but you can use them in queries. For example, with Json Fields=customers.address, the following query returns the address column as {"city":"London","street":"Baker St"}:

    SELECT name, address FROM customers WHERE "address.city" = 'London'

    To get a value from a JSON column, use the MONGO_JSON_VALUE(json, path) SQL function. For example, the following expression returns Paris for a document with "city": "Paris" in the address field:

    MONGO_JSON_VALUE(address, '$.city')