1

I've made some search but i can't understant how it works.

The mongo C# driver is using an _t to store the type so when i deserialize i can know wich subclass to use, but i'm trying to understand how it works if i want to use the same DB with PHP or another programming langage.

I'm afraid it would make deserialization impossible because of that _t type. If someone can explain me how all this works, i've tried to get answers on web but it's still partial.

Here is one record example i get with mongodb and c# :

{
    "_id" : "7aafd454629944c3a1a4b6a9c80db677",
    "_t" : "a-WhereIsFrom",
    "clickfrom" : "here",
    "whichwidget" : "none",
    "Name" : "Franck"
}
Chris
  • 205
  • 1
  • 4
  • 19
  • 1
    Maybe I misunderstand. Mongo stores/transfers BSON which is binary JSON. Meaning the context itself is JSON. JSON deserialization can happen in any language since it is just a text representation of an object. C# is a statically typed language, and while you can deserialize as `dynamic` thats not good. The `_t` field is just so the Mongo deserialization library can know what objects it was translated from (i.e. if you have an inherited class `FooBar : Foo`, the driver knows if it needs to translate the object as `Foo` or as `FooBar`. You're just trying to understand how that works? – Ricky Hartmann May 16 '17 at 09:30
  • Yeah this seems legit to me, i've been arguing about this with some friend and came up with the same explication. I think, like you said, that the _t is usefull to C# wich is typed but for others langage it could, like you said too, work since json deserialization can happen in any language. I'm trying to understand how it works yes but to finaly make it with php ! – Chris May 16 '17 at 09:38

1 Answers1

1

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.

Community
  • 1
  • 1
Ricky Hartmann
  • 891
  • 1
  • 7
  • 20
  • Thanks a lot for those explications. Hope the Q/A will be usefull to other people ! The PHP function you wrote can be used for example to unserialize data from c# integration ? so foo would be my _t if i understand – Chris May 16 '17 at 13:30
  • 1
    Had the variables mixed up. `Bar` was the property name in the C# object, so I updated to match. You could potentially use the `_t` provided in the object to help you understand what to translate to in a different language as well. It's entirely up to you how you'd go about it, but at least in C# its nice to let the driver do the messy logic of interpreting what property should associate to the data document. There are also annotations that allow you to be more specific on what to name each property, what to ignore, what is required, etc. https://blog.oz-code.com/how-to-mongodb-in-c-part-1/ – Ricky Hartmann May 16 '17 at 13:46
  • I get it ! This way both serialization and deserialisation works with c# – Chris May 16 '17 at 15:10