I have had a good look around but I can't seem to find the answer to my question. If I have the following document with several different embedded document types, how do I set the type map for each document independently of the others?
{
_id : ObjectId("bf98321fba31233"),
name : "Example",
comments : [
comment : { _id : ObjectId("bf98321fba31233"), text : "Example comment type" },
comment : { _id : ObjectId("bf98321fba31233"), text : "Another example" }
],
attachments : [
attachment : { _id : ObjectId("bf98321fba31233"), url : "/var/www/blah/foo" },
attachment : { _id : ObjectId("bf98321fba31233"), url : "/var/www/blah/bar" }
]
}
I know how I can set a type map for all embedded documents using the PHP7 MongoDB driver:
$cursor = $mongo->executeQuery("TestDB.TestCollection", new MongoDB\Driver\Query([]));
$cursor->setTypeMap(['root' => 'RootObj', 'document' => 'SomeOtherObj']);
This will unserialize all of the embedded documents into instances of the SomeOtherObj class, and the root document into the RootObj class. What I really want to do is specify this for each type of embedded document.
$cursor->setTypeMap(['root' => 'RootObj', 'comment' => 'CommentObj', 'attachment' => 'AttachmentObj']);
Can it be done?