The table below contains type mapping rules that are used to map BSON types to .NET types.
BSON Type |
MongoDBType |
SQL Engine Column |
.NET Data Type |
|---|---|---|---|
| Double | Double | DOUBLE | Double |
| Double in a column declared SINGLE or FLOAT4 (CREATE TABLE, EnsureCreated) | Double | SINGLE | Single |
| String | String | VARCHAR | String |
| 32-bit integer | Integer | INT | Int32 |
| 32-bit integer in a column declared TINYINT or SMALLINT (CREATE TABLE, EnsureCreated) | Integer | TINYINT, SMALLINT | Byte, Int16 |
| 64-bit integer | Long | INT64 | Int64 |
| Decimal128 | Decimal128 | DECIMAL128 | String by default; Decimal or Double by Decimal128 Mapping |
| Boolean | Boolean | BOOLEAN | Boolean |
| Date | Date | DATETIME | DateTime, Kind by DateTime Kind (Utc by default) |
| ObjectId | Oid | OID | String (24 hexadecimal digits) |
| Binary data, subtype 4 (or 3, by Guid Format) | Uuid | UUID | Guid |
| Other binary data | Binary | part columns .base64 and .subType | String |
| Date with offset, stored by DateTimeOffset Format | DateTimeOffset | DATETIMEOFFSET | DateTimeOffset |
| Embedded document | Object | A column for each nested field; one VARCHAR with JSON with Complex As String or Json Fields | String (JSON) |
| Array | Array | A column per element (tags.0); one VARCHAR with JSON with Complex As String or Json Fields | String (JSON) |
| Regular expression | Regex | Part columns .pattern and .options | String |
| JavaScript | Code | Part column .$code | String |
| JavaScript with scope | ScopeCode | Part columns .$code, .$scope | String |
| Timestamp | TimeStamp | Part columns .t and .i | String; parts Int32 |
| Null | Null | MNULL | String (the value is DBNull) |
| MinKey, MaxKey | MinKey, MaxKey | MINKEY, MAXKEY | String |
Notes:
Uuid and DateTimeOffset are not BSON types; they describe how the provider reads a binary Guid and a stored DateTimeOffset, respectively.CREATE TABLE or EF Core EnsureCreated retains its declared type in the collection schema. TINYINT, SMALLINT, and SINGLE columns are read as Byte, Int16, and Single, respectively (GetDataTypeName returns TINYINT, SMALLINT, SINGLE). The values are stored as 32-bit integers and doubles. A stored value that doesn't fit the declared type is returned by GetValue as stored, while GetByte, GetInt16, and GetFloat throw an InvalidCastException with the message: "The value 300 of the column "b" can't be converted to Byte".{DateTime, Ticks, Offset} as the official MongoDB driver writes it), Array ([ticks, offset in minutes]), or String (ISO 8601). All these formats are supported for reading. In SQL, such values are compared and sorted by the instant.OverflowException. Double preserves NaN and Infinity.Decimal128 values are sorted numerically in SQL (1.5 equals 1.50).Complex As String, the JSON uses relaxed Extended JSON: numbers are represented as plain values (5, not {"$numberLong":"5"}), while ObjectId and date values retain the {"$oid": ...} and {"$date": ...} formats..NET Data Type |
BSON Type Written |
|---|---|
| Byte, SByte, Int16, UInt16, Int32, UInt32 | 32-bit integer |
| Int64, UInt64 | 64-bit integer |
| Single, Double | Double |
| Decimal | Decimal128 |
| Boolean | Boolean |
| DateTime | Date; a DateTime of Unspecified kind is taken as DateTime Kind |
| DateTimeOffset | By DateTimeOffset Format |
| Guid | By Guid Format (binary subtype 4 by default) |
| String | String; ObjectId when MongoDBType=Oid |
| Byte[] with MongoDBType=Binary | Binary data, subtype 0 |
| MongoDBDocument | Embedded document |
| Null | Null |
Arrays can't be passed as parameters. A Byte[] value requires MongoDBType=Binary to be set explicitly.