There are two ways to access metadata with dotConnect for MongoDB: using the GetSchema method of MongoDBConnection or querying the information schema tables, described in the Information Schema topic. This topic describes the GetSchema method.
dotConnect for MongoDB provides the GetSchema method of MongoDBConnection for getting MongoDB metadata. The method is defined by the System.Data.Common.DbConnection class and follows the standard ADO.NET GetSchema API. It allows you to read server schema information without writing queries and parsing the output. You can get information about databases (catalogs), collections (tables), fields (columns), indexes, and keys. MongoDB has no foreign keys, so there are no metadata collections for them.
This article consists of the following sections:
The GetSchema method has three overloads. All overloads return a System.Data.DataTable object that contains information about server elements.
If you call the GetSchema method without parameters, or with the single parameter "MetaDataCollections" (which is the same), the returned table contains the list of all metadata collections. The first column of each row is a collection name that you can pass to the method as the collectionName argument. The second column is the number of restriction values for this collection.
GetSchema with one argument returns general information about the specified metadata collection. For example, GetSchema("Tables") returns the list of the collections of the MongoDB server.
In this overload, the first parameter is the name of a metadata collection, and the second parameter is the array of restrictions to apply when querying information. The restrictions specify which subset of the collection to return, for example, only the collections of a certain database. The number and description of the restrictions allowed for each metadata collection are provided in the table below. Their number can also be obtained from the result of the GetSchema() method. If the second parameter is null, it is ignored. A restriction with a null value matches all values; for example, if the database restriction is null, the metadata of all databases of the server is returned. In the Catalogs collection (CatalogName), in the Tables collection (TableName), and in the Columns collection (ColumnName), a restriction can contain the wildcards '%' (any number of characters) and '_' (one character).
Instead of specifying the metadata collection name as a string constant, you can use the members of System.Data.DbMetaDataCollectionNames as the first argument of the GetSchema method. The members of this class are string constants that correspond to the names of the metadata collections.
The following table provides detailed information on the metadata collections that can be retrieved using the GetSchema method, and the restrictions that can be applied to them. When calling the GetSchema method, you can pass all or some of the restrictions. A restriction that you do not specify, or specify as null, matches all values.
Collection Name |
Number of restrictions |
Remarks |
|---|---|---|
| MetaDataCollections | 0 |
Returns the list of supported metadata collections. Same as using GetSchema() method without parameters. |
| ReservedWords | 0 |
SQLite keywords in SQL mode; empty in native mode. For more information, see SQL Mode and Native Mode. |
| DataSourceInformation | 0 |
Product "MongoDB", case-insensitive identifiers in SQL mode, parameter marker |
| DataTypes | 0 |
The 18 BSON types: double, string, object, array, binData, objectId, bool, date, null, regex, javascript, javascriptWithScope, int, timestamp, long, decimal, minKey, maxKey. |
| Restrictions | 0 |
Returns the restrictions of the collections. |
| Catalogs | 1 |
Lists the databases of the server. Restriction: CatalogName. |
| Tables | 2 |
Lists the collections and views; system collections are skipped unless the TableName restriction specifies them. Restrictions: CatalogName, TableName. Columns: CatalogName, TableName, TableType. |
| Columns | 3 |
Lists the fields: those of the $jsonSchema validator of the collection and those of the sampled documents. A collection the user may not read (for example config.system.sessions) is skipped. Restrictions: CatalogName, TableName, ColumnName. |
| Indexes | 3 |
Lists the indexes of the collections as MongoDB has them; the index of _id is named _id_. Restrictions: CatalogName, TableName, IndexName. Columns: CatalogName, TableName, IndexName, IsPrimary, IsUnique. |
| PrimaryKeys | 3 |
The |
| UniqueKeys | 3 |
The unique indexes, _id_ included. Restrictions: CatalogName, TableName, KeyName. |
| IndexColumns | 4 |
Lists the fields of each index in key order (Position from 1). Restrictions: CatalogName, TableName, IndexName, ColumnName. |
For each metadata collection, dotConnect for MongoDB returns a DataTable with its own set of columns.
Columns
For the Columns collection, the result DataTable contains the following columns:
Column Name |
Data type |
Description |
|---|---|---|
| CatalogName | string | The database that contains the collection. |
| TableName | string | The collection that contains the field. |
| ColumnName | string | The name of the field. The fields of embedded documents are named with a dot, for example address.city. |
| ColumnOrdinal | int | The position of the column in the table. |
| ColumnSize | int | The size of the column. -1 if the size is not known. If the validator of the collection declares the length of the column type, the declared length. |
| NumericPrecision | short | The numeric precision of the column. -1 if not applicable. |
| NumericScale | short | The scale of the column. -1 if not applicable. |
| DataType | System.Type | The .NET data type of the column. |
| ProviderType | int | The provider type of the column. |
| IsLong | bool | Indicates whether the column is long. True for binary columns (binData), false for the others. |
| AllowDBNull | bool | Indicates whether the column can contain NULL values. False for _id and for the fields that the $jsonSchema validator of the collection lists as required, if MongoDB refuses documents without them. |
| IsReadOnly | bool | Indicates whether the column is read-only. Always false. |
| IsRowVersion | bool | Indicates whether the column is a row version. True for columns of the BSON timestamp type. |
| IsUnique | bool | Indicates whether the column values must be unique: _id and a field covered alone by a unique index (not a partial or compound one). |
| IsKey | bool | Indicates whether the column is a part of the primary key. True for _id. |
| IsAutoIncrement | bool | Indicates whether the column values are generated automatically. Always false. |
Tables
For the Tables collection, the result DataTable contains the following columns:
Column Name |
Data type |
Description |
|---|---|---|
| CatalogName | string | The database that contains the collection. |
| TableName | string | The name of the collection or view. |
| TableType | string | The type of the table: table for a collection, view for a view. |
MetaDataCollections
For the MetaDataCollections collection, the result DataTable contains the following columns:
Column Name |
Data type |
Description |
|---|---|---|
| CollectionName | string | The name of the metadata collection. |
| NumberOfRestrictions | int | The number of restrictions of the collection. |
| NumberOfIdentifierParts | int | The number of parts of the identifier. Always 0. |
ReservedWords
For the ReservedWords collection, the result DataTable contains the following column:
Column Name |
Data type |
Description |
|---|---|---|
| Name | string | The reserved word. |
DataSourceInformation and DataTypes
The DataSourceInformation and DataTypes collections return the standard columns of these metadata collections of ADO.NET.
Restrictions
For the Restrictions collection, the result DataTable contains the following columns:
Column Name |
Data type |
Description |
|---|---|---|
| CollectionName | string | The name of the metadata collection. |
| RestrictionName | string | The name of the restriction. |
| RestrictionDefault | string | The default value, used if the restriction is not specified. |
| RestrictionNumber | int | The number of the restriction, starting from 0. |
Catalogs
For the Catalogs collection, the result DataTable contains the following column:
Column Name |
Data type |
Description |
|---|---|---|
| CatalogName | string | The name of the database. |
Indexes, PrimaryKeys, and UniqueKeys
For the Indexes, PrimaryKeys, and UniqueKeys collections, the result DataTable contains the following columns:
Column Name |
Data type |
Description |
|---|---|---|
| CatalogName | string | The database that contains the collection. |
| TableName | string | The collection that contains the index. |
| IndexName | string | The name of the index. The index of _id is named _id_. |
| IsPrimary | bool | Indicates whether the index is the _id index. |
| IsUnique | bool | Indicates whether the index is unique. |
For PrimaryKeys and UniqueKeys, the restriction with the name of the index is called KeyName, but the result column with the name of the index is IndexName in all three collections.
IndexColumns
For the IndexColumns collection, the result DataTable contains the following columns:
Column Name |
Data type |
Description |
|---|---|---|
| CatalogName | string | The database that contains the collection. |
| TableName | string | The collection that contains the index. |
| IndexName | string | The name of the index. |
| ColumnName | string | The name of the field in the index key. |
| Position | int | The position of the field in the index key, starting from 1. |
| ColumnSize, NumericPrecision, NumericScale, DataType, ProviderType, IsLong, AllowDBNull, IsReadOnly, IsRowVersion, IsUnique, IsKey, IsAutoIncrement | The properties of the column of the same name in the Columns collection. If the collection has no such column, the default values are returned (for example, DataType is System.Object). |
The following example returns the columns of the customers collection of the sample database. The first restriction of the Columns collection is the database. If it is null, the columns of all databases of the server are returned, so the database is specified explicitly in this example.
For documents of the { name, address: { city, street } } structure, the example outputs the following:
The following example lists the databases of the server.
DataTable catalogs = connection.GetSchema("Catalogs");
foreach (DataRow row in catalogs.Rows)
Console.WriteLine(row["CatalogName"]); |
The following example lists the collections of the sample database. The first restriction of the Tables collection is the database name.
DataTable tables = connection.GetSchema("Tables", new string[] { "sample" });
foreach (DataRow row in tables.Rows)
Console.WriteLine("{0} ({1})", row["TableName"], row["TableType"]); |
The following example retrieves the indexes of the customers collection. The restrictions are the database name and the collection name; a null value for a restriction matches all values.