pub struct Document { /* private fields */ }
Expand description
A BSON document represented as an associative HashMap with insertion ordering.
Implementations§
Source§impl Document
impl Document
Sourcepub fn get(&self, key: impl AsRef<str>) -> Option<&Bson>
pub fn get(&self, key: impl AsRef<str>) -> Option<&Bson>
Returns a reference to the Bson corresponding to the key.
Sourcepub fn get_mut(&mut self, key: impl AsRef<str>) -> Option<&mut Bson>
pub fn get_mut(&mut self, key: impl AsRef<str>) -> Option<&mut Bson>
Gets a mutable reference to the Bson corresponding to the key
Sourcepub fn get_f64(&self, key: impl AsRef<str>) -> ValueAccessResult<f64>
pub fn get_f64(&self, key: impl AsRef<str>) -> ValueAccessResult<f64>
Get a floating point value for this key if it exists and has the correct type.
Sourcepub fn get_f64_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut f64>
pub fn get_f64_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut f64>
Get a mutable reference to a floating point value for this key if it exists and has the correct type.
Sourcepub fn get_decimal128(
&self,
key: impl AsRef<str>,
) -> ValueAccessResult<&Decimal128>
pub fn get_decimal128( &self, key: impl AsRef<str>, ) -> ValueAccessResult<&Decimal128>
Get a reference to a Decimal128 value for key, if it exists.
Sourcepub fn get_decimal128_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut Decimal128>
pub fn get_decimal128_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut Decimal128>
Get a mutable reference to a Decimal128 value for key, if it exists.
Sourcepub fn get_str(&self, key: impl AsRef<str>) -> ValueAccessResult<&str>
pub fn get_str(&self, key: impl AsRef<str>) -> ValueAccessResult<&str>
Get a string slice this key if it exists and has the correct type.
Sourcepub fn get_str_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut str>
pub fn get_str_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut str>
Get a mutable string slice this key if it exists and has the correct type.
Sourcepub fn get_array(&self, key: impl AsRef<str>) -> ValueAccessResult<&Array>
pub fn get_array(&self, key: impl AsRef<str>) -> ValueAccessResult<&Array>
Get a reference to an array for this key if it exists and has the correct type.
Sourcepub fn get_array_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut Array>
pub fn get_array_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut Array>
Get a mutable reference to an array for this key if it exists and has the correct type.
Sourcepub fn get_document(&self, key: impl AsRef<str>) -> ValueAccessResult<&Document>
pub fn get_document(&self, key: impl AsRef<str>) -> ValueAccessResult<&Document>
Get a reference to a document for this key if it exists and has the correct type.
Sourcepub fn get_document_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut Document>
pub fn get_document_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut Document>
Get a mutable reference to a document for this key if it exists and has the correct type.
Sourcepub fn get_bool(&self, key: impl AsRef<str>) -> ValueAccessResult<bool>
pub fn get_bool(&self, key: impl AsRef<str>) -> ValueAccessResult<bool>
Get a bool value for this key if it exists and has the correct type.
Sourcepub fn get_bool_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut bool>
pub fn get_bool_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut bool>
Get a mutable reference to a bool value for this key if it exists and has the correct type.
Sourcepub fn get_i32(&self, key: impl AsRef<str>) -> ValueAccessResult<i32>
pub fn get_i32(&self, key: impl AsRef<str>) -> ValueAccessResult<i32>
Get an i32 value for this key if it exists and has the correct type.
Sourcepub fn get_i32_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut i32>
pub fn get_i32_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut i32>
Get a mutable reference to an i32 value for this key if it exists and has the correct type.
Sourcepub fn get_i64(&self, key: impl AsRef<str>) -> ValueAccessResult<i64>
pub fn get_i64(&self, key: impl AsRef<str>) -> ValueAccessResult<i64>
Get an i64 value for this key if it exists and has the correct type.
Sourcepub fn get_i64_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut i64>
pub fn get_i64_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut i64>
Get a mutable reference to an i64 value for this key if it exists and has the correct type.
Sourcepub fn get_timestamp(
&self,
key: impl AsRef<str>,
) -> ValueAccessResult<Timestamp>
pub fn get_timestamp( &self, key: impl AsRef<str>, ) -> ValueAccessResult<Timestamp>
Get a time stamp value for this key if it exists and has the correct type.
Sourcepub fn get_timestamp_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut Timestamp>
pub fn get_timestamp_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut Timestamp>
Get a mutable reference to a time stamp value for this key if it exists and has the correct type.
Sourcepub fn get_binary_generic(
&self,
key: impl AsRef<str>,
) -> ValueAccessResult<&Vec<u8>>
pub fn get_binary_generic( &self, key: impl AsRef<str>, ) -> ValueAccessResult<&Vec<u8>>
Get a reference to a generic binary value for this key if it exists and has the correct type.
Sourcepub fn get_binary_generic_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut Vec<u8>>
pub fn get_binary_generic_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut Vec<u8>>
Get a mutable reference generic binary value for this key if it exists and has the correct type.
Sourcepub fn get_object_id(&self, key: impl AsRef<str>) -> ValueAccessResult<ObjectId>
pub fn get_object_id(&self, key: impl AsRef<str>) -> ValueAccessResult<ObjectId>
Get an object id value for this key if it exists and has the correct type.
Sourcepub fn get_object_id_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut ObjectId>
pub fn get_object_id_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut ObjectId>
Get a mutable reference to an object id value for this key if it exists and has the correct type.
Sourcepub fn get_datetime(&self, key: impl AsRef<str>) -> ValueAccessResult<&DateTime>
pub fn get_datetime(&self, key: impl AsRef<str>) -> ValueAccessResult<&DateTime>
Get a reference to a UTC datetime value for this key if it exists and has the correct type.
Sourcepub fn get_datetime_mut(
&mut self,
key: impl AsRef<str>,
) -> ValueAccessResult<&mut DateTime>
pub fn get_datetime_mut( &mut self, key: impl AsRef<str>, ) -> ValueAccessResult<&mut DateTime>
Get a mutable reference to a UTC datetime value for this key if it exists and has the correct type.
Sourcepub fn contains_key(&self, key: impl AsRef<str>) -> bool
pub fn contains_key(&self, key: impl AsRef<str>) -> bool
Returns true if the map contains a value for the specified key.
Sourcepub fn insert<KT: Into<String>, BT: Into<Bson>>(
&mut self,
key: KT,
val: BT,
) -> Option<Bson>
pub fn insert<KT: Into<String>, BT: Into<Bson>>( &mut self, key: KT, val: BT, ) -> Option<Bson>
Sets the value of the entry with the OccupiedEntry’s key, and returns the entry’s old value. Accepts any type that can be converted into Bson.
Sourcepub fn remove(&mut self, key: impl AsRef<str>) -> Option<Bson>
pub fn remove(&mut self, key: impl AsRef<str>) -> Option<Bson>
Takes the value of the entry out of the document, and returns it. Computes in O(n) time (average).
pub fn entry(&mut self, k: String) -> Entry<'_>
Sourcepub fn to_writer<W: Write>(&self, writer: W) -> Result<()>
pub fn to_writer<W: Write>(&self, writer: W) -> Result<()>
Attempts to serialize the Document
into a byte stream.
While the method signature indicates an owned writer must be passed in, a mutable reference
may also be passed in due to blanket implementations of Write
provided in the standard
library.
use bson::doc;
let mut v: Vec<u8> = Vec::new();
let doc = doc! { "x" : 1 };
doc.to_writer(&mut v)?;
Sourcepub fn from_reader<R: Read>(reader: R) -> Result<Document>
pub fn from_reader<R: Read>(reader: R) -> Result<Document>
Attempts to deserialize a Document
from a byte stream.
While the method signature indicates an owned reader must be passed in, a mutable reference
may also be passed in due to blanket implementations of Read
provided in the standard
library.
use bson::{doc, Document};
use std::io::Cursor;
let mut v: Vec<u8> = Vec::new();
let doc = doc! { "x" : 1 };
doc.to_writer(&mut v)?;
// read from mutable reference
let mut reader = Cursor::new(v.clone());
let doc1 = Document::from_reader(&mut reader)?;
// read from owned value
let doc2 = Document::from_reader(Cursor::new(v))?;
assert_eq!(doc, doc1);
assert_eq!(doc, doc2);
Sourcepub fn from_reader_utf8_lossy<R: Read>(reader: R) -> Result<Document>
pub fn from_reader_utf8_lossy<R: Read>(reader: R) -> Result<Document>
Attempt to deserialize a Document
that may contain invalid UTF-8 strings from a byte
stream.
This is mainly useful when reading raw BSON returned from a MongoDB server, which
in rare cases can contain invalidly truncated strings (https://jira.mongodb.org/browse/SERVER-24007).
For most use cases, Document::from_reader
can be used instead.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Document
impl<'de> Deserialize<'de> for Document
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value given this Deserializer
.
Source§impl Extend<(String, Bson)> for Document
impl Extend<(String, Bson)> for Document
Source§fn extend<T: IntoIterator<Item = (String, Bson)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (String, Bson)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<'a> IntoIterator for &'a Document
impl<'a> IntoIterator for &'a Document
Source§impl IntoIterator for Document
impl IntoIterator for Document
Source§impl TryFrom<&RawDocument> for Document
impl TryFrom<&RawDocument> for Document
Source§impl TryFrom<Map<String, Value>> for Document
This converts from the input JSON as if it were MongoDB Extended JSON v2.
impl TryFrom<Map<String, Value>> for Document
This converts from the input JSON as if it were MongoDB Extended JSON v2.