更新时间: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")