dotConnect for MongoDB provides metadata tables, compliant with the SQL-92 information schema standard, that describe the collections and views of the database of the connection. Collections are mapped to tables and views to views. Fields, including the fields of embedded documents and the elements of arrays, are mapped to columns. The _id field is mapped to the primary key, and the indexes of MongoDB are listed with their fields. MongoDB has no foreign keys. The following metadata tables are available:
The metadata tables are available in SQL mode (Sql Engine=true, the default). They describe the database specified in the Database connection string parameter: TABLE_CATALOG holds its name, and TABLE_SCHEMA is always NULL. Collections of other databases of the server and system collections (system.*) are not listed; use the GetSchema method to get the metadata of another database. The metadata tables are read-only.
The metadata is read from the server each time a query uses these tables, so a collection created after the connection was opened is listed at once. SYS_COLUMNS lists the columns as the queries of the connection see them.
You may query these tables in the same way you query collections, with joins, conditions and parameters, and you may use metadata tables and collections in one query.
Here are examples of such queries.
The following query returns the list of tables and their columns, ordered in the same way they are ordered in their tables:
The following query displays the tables with their primary key constraints and the key columns:
SELECT C.TABLE_NAME, C.CONSTRAINT_NAME, U.COLUMN_NAME FROM SYS_TABLE_CONSTRAINTS AS C, SYS_KEY_COLUMN_USAGE AS U WHERE C.CONSTRAINT_NAME = U.CONSTRAINT_NAME ORDER BY C.TABLE_NAME |
The following query displays the indexes of the customers table with their fields; a compound index has a row for each field:
For a collection with a unique index on name, a unique index on email with a filter, a sparse index on tags and a compound index on address.city ascending and name descending, the query returns:
The following query displays the tables that have a schema validator and its settings:
The following code reads the columns of the table, specified in the table parameter, and their types:
For documents of the { name, email, address: { city, street }, tags: ["vip"] } structure, the example outputs the following: