case class ObjectId(id: Array[Byte]) extends Product with Serializable
BSON's 12-byte ObjectId type, constructed using: a 4-byte value representing the seconds since the Unix epoch, a 3-byte machine identifier, a 2-byte process id, and a 3-byte counter, starting with a random value.
The implementation is adopt from ReactiveMongo.
- Alphabetic
- By Inheritance
- ObjectId
- Serializable
- Product
- Equals
- AnyRef
- Any
- by objectId2JsValue
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- def +(other: String): String
- def ->[B](y: B): (ObjectId, B)
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def apply(range: Range): JsArray
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def apply(start: Int, end: Int, step: Int): JsArray
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def apply(start: Int, end: Int): JsArray
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def apply(index: Int): JsValue
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def apply(key: String): JsValue
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def applyDynamic(key: String)(index: Int): JsValue
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def asBoolean: Boolean
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def asDate: LocalDate
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def asDateTime: LocalDateTime
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def asDecimal: BigDecimal
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def asDouble: Double
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def asInt: Int
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def asLong: Long
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def asTime: LocalTime
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def asTimestamp: Timestamp
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def compactPrint: String
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def ensuring(cond: (ObjectId) => Boolean, msg: => Any): ObjectId
- def ensuring(cond: (ObjectId) => Boolean): ObjectId
- def ensuring(cond: Boolean, msg: => Any): ObjectId
- def ensuring(cond: Boolean): ObjectId
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(that: Any): Boolean
- Definition Classes
- ObjectId → Equals → AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- def get(key: String): Option[JsValue]
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- val id: Array[Byte]
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def prettyPrint: String
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def productElementNames: Iterator[String]
- Definition Classes
- Product
- def remove(index: Int): JsValue
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def remove(key: String): Option[JsValue]
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def selectDynamic(key: String): JsValue
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def timestamp: Date
The timestamp port of ObjectId object as a Date
- def toString(): String
In the form of a string literal "ObjectId(...)"
In the form of a string literal "ObjectId(...)"
- Definition Classes
- ObjectId → AnyRef → Any
- def update(index: Int, value: JsValue): JsValue
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def update(key: String, value: JsValue): JsValue
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def updateDynamic(index: Int)(value: JsValue): JsValue
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- def updateDynamic(key: String)(value: JsValue): JsValue
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsValue
- val value: ObjectId
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Definition Classes
- JsObjectId
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
Shadowed Implicit Value Members
- def equals(o: Any): Boolean
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Shadowing
- This implicitly inherited member is shadowed by one or more members in this class.
To access this member you can use a type ascription:(objectId: JsObjectId).equals(o)
- Definition Classes
- JsObjectId → Equals → AnyRef → Any
- def productElementNames: Iterator[String]
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Shadowing
- This implicitly inherited member is shadowed by one or more members in this class.
To access this member you can use a type ascription:(objectId: JsObjectId).productElementNames
- Definition Classes
- Product
- def toString(): String
- Implicit
- This member is added by an implicit conversion from ObjectId toJsObjectId performed by method objectId2JsValue in smile.json.
- Shadowing
- This implicitly inherited member is shadowed by one or more members in this class.
To access this member you can use a type ascription:(objectId: JsObjectId).toString()
- Definition Classes
- JsObjectId → JsValue → AnyRef → Any
Deprecated Value Members
- def formatted(fmtstr: String): String
- Implicit
- This member is added by an implicit conversion from ObjectId toStringFormat[ObjectId] performed by method StringFormat in scala.Predef.
- Definition Classes
- StringFormat
- Annotations
- @deprecated @inline()
- Deprecated
(Since version 2.12.16) Use
formatString.format(value)
instead ofvalue.formatted(formatString)
, or use thef""
string interpolator. In Java 15 and later,formatted
resolves to the new method in String which has reversed parameters.
- def →[B](y: B): (ObjectId, B)
- Implicit
- This member is added by an implicit conversion from ObjectId toArrowAssoc[ObjectId] performed by method ArrowAssoc in scala.Predef.
- Definition Classes
- ArrowAssoc
- Annotations
- @deprecated
- Deprecated
(Since version 2.13.0) Use
->
instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages,
JSON has only types of string, number, boolean, object, array, and null. This library includes additional types such as date, int, long, double, counter, binary, UUID, ObjectId (as in BSON), etc.
It is very easy to parse a JSON object:
The interpolator
json
parse a string toJsObject
. To parse an array, use the interpolatorjsan
toJsArray
. It is also okay to embed variable references directly in processed string literals.If the string is not a JSON object but any other valid JSON expression, one may use parseJson method to convert the string to a
JsValue
."1".parseJson
The json interpolator can only be applied to string literals. If you want to parse a string variable, the
parseJson
method can always be employed. If you know the string contains a JSON object, you may also use the methodparseJsObject
.To serialize a JSON value (of type JsValue) in compact mode, you can just use
toString
. To pretty print, use the methodprettyPrint
.With a
JsObject
orJsArray
, you can refer to the individual elements with a variation of array syntax, like this:Note that we follow Scala's array access convention by
()
rather than[]
in JavaScript.Besides, you can use the dot notation to access its fields/elements just like in JavaScript:
doc.store.bicycle.color doc.store.book(0).author
It is worth noting that we didn't define the type/schema of the document while Scala is a strong type language. In other words, we have both the type safe features of strong type language and the flexibility of dynamic language in this JSON library.
If you try to access a non-exist field,
JsUndefined
is returned.Although there are already several nice JSON libraries for Scala, the JSON objects are immutable by design, which is a natural choice for a functional language. For database, however, data mutation is necessary. Therefore,
JsObject
andJsArray
are mutable data structures. You can set/add a field just like in JavaScript:json.store.bicycle.color = "green"
To delete a field from
JsObject
, use remove method:It is same as setting it
JsUndefined
:doc.store.book(0).price = `JsUndefined`
To delete an element from
JsArray
, the remove method will effectively remove it from the array. However, setting an element to undefined doesn't reduce the array size.It is also possible to append an element or another array to
JsArray
:Common iterative operations such as
foreach
,map
,reduce
can be applied toJsArray
too.doc.store.book.asInstanceOf[JsArray].foreach { book => println(book.price) }
Because Scala is a static language, it is impossible to know
doc.store.book
is an array at compile time. So it is typed as genericJsValue
, which is the parent type of specific JSON data types. Therefore, we useasInstanceOf[JsArray]
to convert it toJsArray
in order to useforeach
.