Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Scala ドライバー
/

ドキュメント

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 型

Document

org.mongodb.scala.bson.Document

Array

List

Date

Date またはint (エポックからのミリ秒)

Boolean

Boolean

Double

Double

Int32

Integer

Int64

Long

String

String

Binary

Array[Byte]

ObjectId

ObjectId

Null

None

これらのマッピングを変更または拡張することができます。そのプロセスについて、次のセクションで説明します。

次のセクションでは、2 つの主要なDocumentクラスについて説明します。

Scala コレクション ライブラリと同様に、不変クラス が優先クラスです。 便宜上、 org.mongodb.scala.Documentorg.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 型からの明確な直接マッピングがあります。 たとえば、 StringBsonStringにマップされ、 IntBsonInt32にマップされ、 LongBsonInt64にマップされます。 便宜上、これらの型はDocument型で直接使用でき、 BsonMagnetsオブジェクトの契約方式によって変換されます。 特定の型のスコープに暗黙的なBsonTransformerがある限り、その型はBsonValueに変換できます。

次のBsonTransformersはデフォルトでスコープ内にあります。

Scala 型
BsonValue

Boolean

BsonBoolean

String

BsonString

Array[Byte]

BsonBinary

Regex

BsonRegex

Date

BsonDateTime

ObjectId

BsonObjectId

Int

BsonInt32

Long

BsonInt64

Double

BsonDouble

immutable.Document

BsonDocument

mutable.Document

BsonDocument

Option[T]

BsonValue ここで、 は を持ち、TBsonTransformer

Seq[(String, T)]

BsonDocument ここで、 は を持ち、TBsonTransformer

Seq[T]

BsonArray ここで、 は を持ち、TBsonTransformer

BsonValue

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 、( StringBsonValue )、 Iterable[(String, BsonValue)]など、単一の値またはキーと値のペアまたは複数のキーと値のペアを通常期待する API では、次の値になるものはすべて必要です。正しい型に準拠するために必要な暗黙的な変換を処理するCanBeX経由の 型。 これらの特性は、 CanBeBsonValueCanBeBsonElementCanBeBsonElementsです。

たとえば、 Documentまたは値のリストにキーと値のペアを追加する方法は次のとおりです。

val doc1 = Document("AL" -> "Alabama")
val doc2 = Document("codes" -> List("AL", "AK", "AR"))

ドライバーには、 Bsonという小さいが強力なインターフェースも含まれています。 ドライバー自体に含まれているか、サード パーティーに含まれているかにかかわらず、BSON ドキュメントを表す任意のクラスはこのインターフェースを実装でき、BSON ドキュメントが必要な高レベル API の任意の場所で使用できます。 例:

collection.find(Document("x" -> 1))
collection.find(Filters.eq("x", 1))

戻る

BSON の実装

項目一覧