Just to put this in an answer: MongoDB stores its data in BSON (binary JSON). Essentially allowing each record to be interpreted as JSON string, which can be deserialized into any language.
C# is statically typed, and when deserializing something free-form (like JSON), it will try to understand it as a typed object (unless using dynamic
, but I've done that before and it just creates messy code.) The _t
field is meant to allow the Mongo deserializer to understand what type it is supposed to translate it as.
Given:
public class Foo { public string Bar {get;set;} }
public class FooBar : Foo {}
If I'm reading a serialized object that looks like:
[
{ "_t": "Foo", "Bar": "Foo" },
{ "_t": "FooBar", "Bar": "Foo" }
]
It allows the library to return to you a list of respected objects.
If another language is inserting records without the _t
, you can just pull the BsonDocument
and conduct your own lookups, but this is obviously a bit messier.
Translating back into something like PHP, you could have:
class Foo implements MongoDB\BSON\Serializable
{
public $bar;
public function __construct($bar)
{
$this->id = new MongoDB\BSON\ObjectID;
$this->bar = (string) $bar;
}
function bsonSerialize()
{
return [
'_id' => $this->id,
'Bar' => $this->bar,
];
}
function bsonUnserialize(array $data)
{
$this->id = $data['_id'];
$this->bar = $data['Bar'];
}
}
Then use TypeMap to tell the client what to translate each object as.