【发布时间】:2017-09-04 01:51:39
【问题描述】:
我环顾四周,但似乎找不到问题的答案。如果我有以下具有几种不同嵌入文档类型的文档,如何独立于其他文档设置每个文档的类型映射?
{
_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" }
]
}
我知道如何使用PHP7 MongoDB driver 为所有嵌入文档设置类型映射:
$cursor = $mongo->executeQuery("TestDB.TestCollection", new MongoDB\Driver\Query([]));
$cursor->setTypeMap(['root' => 'RootObj', 'document' => 'SomeOtherObj']);
这会将所有嵌入文档反序列化为 SomeOtherObj 类的实例,并将根文档反序列化为 RootObj 类。我真正想做的是为每种类型的嵌入文档指定这个。
$cursor->setTypeMap(['root' => 'RootObj', 'comment' => 'CommentObj', 'attachment' => 'AttachmentObj']);
可以吗?
【问题讨论】:
-
您描述的功能尚未实现。相关的跟踪单是PHPC-314。
-
@jmikola 感谢提醒