且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ReactiveMongo:如何将列表反序列化为Option

更新时间:2023-09-29 09:06:40

为什么要先调用toList,然后再调用flatten?如果我正确地阅读了BSONDocument的文档,则getAs方法将为类型T返回Option,在这种情况下,您将T指定为List[BSONObjectID].调用toList时,基本上是将Option拆包为空的List(对于None)或对于实际的List.然后,这里的flatten似乎没有做任何相关的事情.您不能只按以下方式操作吗?

Why are you calling toList and then flatten? If I'm reading the docs correctly for BSONDocument, the getAs method will return an Option for type T where in your case, you are specifying T as List[BSONObjectID]. When you call toList, you are basically unpacking the Option to either an empty List (in the case of a None) or into the actual List in the case of a Some. And then the flatten here doesn't seem to be doing anything relevant. Can't you just do it as:

doc.getAs[List[BSONObjectID]]("addresses")