-
Notifications
You must be signed in to change notification settings - Fork 1.3k
BsonDocument
mbdavid edited this page Mar 14, 2015
·
8 revisions
BsonDocument
class are LiteDB implementation for documents. Internally, BsonDocument
store key-values in a Dictionary<string, BsonValue>
var customer = new BsonDocument();
customer["_id"] = ObjectId.NewObjectId();
customer["Name"] = "John Doe";
customer["CreateDate"] = DateTime.Now;
customer["Phones"] = new BsonArray { "8000-0000", "9000-000" };
customer["IsActive"] = true;
customer["IsAdmin"] = new BsonValue(true);
customer.Set("Address.Street", "Av. Protasio Alves, 1331");
About document field keys:
- Keys must contains only letters, numbers or
_
and-
- Keys are case-sensitive
- Duplicate keys are not allowed
- LiteDB keeps original key order, including mapped classes. The only exception is for
_id
field that will be always first field.
About document field values:
- Values can be any BSON value type: Null, Int32, Int64, Double, String, Embedded Document, Array, Binary, ObjectId, Guid, Boolean, DateTime, MinValue, MaxValue
- Values has no size limit, but whole document are limited in 1Mb after BSON serialization. This size includes all extra bytes that are used on BSON
- When a field is indexed, the value must be less then 512 bytes after BSON serialize.
-
_id
field cannot be:Null
,MinValue
orMaxValue
-
_id
is unique indexed field, so value must be less then 512 bytes
Abount .NET classes
-
BsonValue
has implicit constructor to all supported .NET data types -
BsonDocument
class are used to hold only documents -
BsonArray
class are used to hold only arrays. Each array item can have a diferent BSON data type -
BsonValue
hasRawValue
property that returns internal .NET object instance.
// Testing BSON value data type
if(customer["Name"].IsString) { ... }
// Helper to get .NET type
string str = customer["Name"].AsString;
To use others .NET data types you need BsonMapper
class.
Data Modeling
- Data Structure
- BsonDocument
- Object Mapping
- Relationships with Document References
- Collections
- FileStorage
Index
Query
Database
Version 4 changes
Shell