#readwise # Object Mapping - LiteDB :: A .NET Embedded NoSQL Database ![rw-book-cover](https://readwise-assets.s3.amazonaws.com/static/images/article4.6bc1851654a0.png) ## Metadata - Author: [[litedb.org]] - Full Title: Object Mapping - LiteDB :: A .NET Embedded NoSQL Database - URL: http://www.litedb.org/docs/object-mapping/ ## Highlights - BsonMapper.ToDocument() auto converts each property of a class to a document field following these conventions: - Properties can be read-only or read/write - The class should have an `Id` property, `<ClassName>Id` property, a property with `[BsonId]` attribute or mapped by the fluent API. - A property can be decorated with `[BsonIgnore]` in order not to be mapped to a document field - A property can be decorated with `[BsonField("fieldName")]` to customize the name of the document field - No circular references are allowed - By default, max depth of 20 inner classes (this can be changed in the BsonMapper) - You can use BsonMapper global instance (BsonMapper.Global) or a custom instance and pass to LiteDatabase in its constructor. Keep this instance in a single place to avoid re-creating the mappings each time you use a database. - In addition to basic BSON types, BsonMapper maps others .NET types to BSON data type: - `Nullable<T>` are accepted. If value is null the BSON type is Null, otherwise the mapper will use `T?`. - For IDictionary<K, T>, K key must be String or a simple type (convertible using Convert.ToString(..)). | .NET type | BSON type | | -------------------------- | --------- | | Int16, UInt16, Byte, SByte | Int32 | | UInt32 , UInt64 | Int64 | | Single | Double | | Char, Enum | String | | ` IList<T>`, `T[]` | Array | | `IDictionary<K,T>` | Document | | Any other .NET type | Document |