Overview
The Scala driver includes two Scala-specific representations for BSON documents. Following the convention from the Scala collections library, there are immutable and mutable implementations of the Document
type. The underlying implementations of Document
use the type-safe BsonDocument class. The BSON classes are available from the org.mongodb.scala.bson
namespace, which includes type aliases and companion objects. These objects should suffice for many use cases, but for advanced use cases you may need to use classes from the org.bson
namespace directly.
重要
重複するキー名
ドキュメント内の重複キー名に関するサーバーの動作は未定義です。 重複キー名を持つドキュメントがデコードされると、ドライバーは重複キーに関連付けられた最後の値を割り当てます。 このようなドキュメントを保存すると、他の 値が失われます。
注意
Scala Document
クラスはTraversableLike[(String,
BsonValue)]
を実装し、一般的な API はMap[String,
BsonValue]
値のそれをミラーリングします。 ただし、 Map
とは異なり、 TraversableLike
の実装では値の型に違いがないため、厳密な型の安全性が確保されます。
BsonValue
は、 org.bson
ライブラリからの BSON 型の型セーフな表現であり、特定の値の型を表します。 最も一般的に使用される値の型は次のとおりです。
BSON 型 | Scala 型 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
これらのマッピングを変更または拡張することができます。そのプロセスについて、次のセクションで説明します。
次のセクションでは、2 つの主要なDocument
クラスについて説明します。
不変のドキュメント
Scala コレクション ライブラリと同様に、不変クラス が優先クラスです。 便宜上、 org.mongodb.scala.Document
とorg.mongodb.scala.bson.Document
のエイリアスになるだけでなく、 org.mongodb.scala.bson.collection.immutable.Document
から利用することもできます。
この型のインスタンスは、すべてのユーザーに対して不変であることが保証されます。 このようなコレクションは、作成された後は変更されません。 したがって、時間の異なる点で同じコレクション値に繰り返しアクセスすると、常に同じ要素を含むコレクションが生成されるという結果に依存できます。
import org.mongodb.scala.bson._ val doc1 = Document("AL" -> BsonString("Alabama")) val doc2 = doc1 + ("AK" -> BsonString("Alaska")) val doc3 = doc2 ++ Document("AR" -> BsonString("Arkansas"), "AZ" -> BsonString("Arizona"))
可変ドキュメント
可変のDocument
型を取得するには、 org.mongodb.scala.collections.mutable.Document
から明示的にインポートする必要があります。 ミューテーション可能なDocument
は、その場で更新または拡張できます。 つまり、 Document
の要素を変更、追加、削除することができるようになります。 Scala コレクションと同様に、可変型を処理する場合は、どのコードがどのコレクションをいつ変更するかを理解する必要があります。
import org.mongodb.scala.bson._ import org.mongodb.scala.bson.collection.mutable.Document val doc = Document("AL" -> BsonString("Alabama")) val doc1 = doc + ("AK" -> BsonString("Alaska")) // doc not mutated but new doc created doc1 ++= Document("AR" -> BsonString("Arkansas"), "AZ" -> BsonString("Arizona")) // doc1 mutated as ++= changes in place.
暗黙的な変換
BsonValue
型の多くには、Scala 型からの明確な直接マッピングがあります。 たとえば、 String
はBsonString
にマップされ、 Int
はBsonInt32
にマップされ、 Long
はBsonInt64
にマップされます。 便宜上、これらの型はDocument
型で直接使用でき、 BsonMagnets
オブジェクトの契約方式によって変換されます。 特定の型のスコープに暗黙的なBsonTransformer
がある限り、その型はBsonValue
に変換できます。
次のBsonTransformers
はデフォルトでスコープ内にあります。
Scala 型 | BsonValue |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.mongodb.scala.Document val doc1 = Document("AL" -> "Alabama") val doc2 = doc1 + ("AK" -> "Alaska") val doc3 = doc2 ++ Document("AR" -> "Arkansas", "population" -> 2.966)
This is achieved by making use of the Magnet Pattern, which you can learn more about in the Magnet Pattern blog post on spray.io.
API では、 BsonValue
、( String
、 BsonValue
)、 Iterable[(String, BsonValue)]
など、単一の値またはキーと値のペアまたは複数のキーと値のペアを通常期待する API では、次の値になるものはすべて必要です。正しい型に準拠するために必要な暗黙的な変換を処理するCanBeX
経由の 型。 これらの特性は、 CanBeBsonValue
、 CanBeBsonElement
、 CanBeBsonElements
です。
たとえば、 Document
または値のリストにキーと値のペアを追加する方法は次のとおりです。
val doc1 = Document("AL" -> "Alabama") val doc2 = Document("codes" -> List("AL", "AK", "AR"))
Bson
ドライバーには、 Bson
という小さいが強力なインターフェースも含まれています。 ドライバー自体に含まれているか、サード パーティーに含まれているかにかかわらず、BSON ドキュメントを表す任意のクラスはこのインターフェースを実装でき、BSON ドキュメントが必要な高レベル API の任意の場所で使用できます。 例:
collection.find(Document("x" -> 1)) collection.find(Filters.eq("x", 1))