You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[`bson!`](https://docs.rs/bson/latest/bson/macro.bson.html) has supports both array and object literals, and it automatically converts any values specified to [`Bson`](https://docs.rs/bson/latest/bson/enum.Bson.html), provided they are `Into<Bson>`.
85
+
86
+
#### [`Bson`](https://docs.rs/bson/latest/bson/enum.Bson.html) value unwrapping
36
87
37
-
Serialize the struct:
88
+
[`Bson`](https://docs.rs/bson/latest/bson/enum.Bson.html) has a number of helper methods for accessing the underlying native Rust types. These helpers can be useful in circumstances in which the specific type of a BSON value
mongoCollection.insert_one(document, None)?; // Insert into a MongoDB collection
52
-
} else {
53
-
println!("Error converting the BSON object into a MongoDB document");
54
-
}
103
+
BSON documents are ordered maps of UTF-8 encoded strings to BSON values. They are logically similar to JSON objects in that they can contain subdocuments, arrays, and values of several different types. This crate models BSON documents via the
[`doc!`](https://docs.rs/bson/latest/bson/macro.doc.html) works similarly to [`bson!`](https://docs.rs/bson/latest/bson/macro.bson.html), except that it always
121
+
returns a [`Document`](https://docs.rs/bson/latest/bson/document/struct.Document.html) rather than a [`Bson`](https://docs.rs/bson/latest/bson/enum.Bson.html).
122
+
123
+
#### [`Document`](https://docs.rs/bson/latest/bson/document/struct.Document.html) member access
56
124
57
-
Deserialize the struct:
125
+
[`Document`](https://docs.rs/bson/latest/bson/document/struct.Document.html) has a number of methods on it to facilitate member
126
+
access:
58
127
59
128
```rust
60
-
usebson::doc;
129
+
letdoc=doc! {
130
+
"string":"string",
131
+
"bool":true,
132
+
"i32":5,
133
+
"doc": { "x":true },
134
+
};
61
135
62
-
//Read the document from a MongoDB collection
63
-
letperson_document=mongoCollection.find_one(Some(doc! { "_id":bson::oid::ObjectId::with_string("12345").expect("Id not valid") }),None)?
// Do things just like with any other Rust data structure.
179
+
println!("Redacting {}'s record.", person.name);
180
+
person.name ="REDACTED".to_string();
181
+
182
+
// Get a serialized version of the input data as a `Bson`.
183
+
letredacted_bson=bson::to_bson(&person).unwrap();
184
+
}
185
+
```
186
+
187
+
Any types that implement `Serialize` and `Deserialize` can be used in this way. Doing so helps
188
+
separate the "business logic" that operates over the data from the (de)serialization logic that
189
+
translates the data to/from its serialized form. This can lead to more clear and concise code
190
+
that is also less error prone.
191
+
70
192
## Breaking Changes
71
193
72
194
In the BSON specification, _unsigned integer types_ are unsupported; for example, `u32`. In the older version of this crate (< `v0.8.0`), if you uses `serde` to serialize _unsigned integer types_ into BSON, it will store them with `Bson::FloatingPoint` type. From `v0.8.0`, we removed this behavior and simply returned an error when you want to serialize _unsigned integer types_ to BSON. [#72](https://github.com/zonyitoo/bson-rs/pull/72)
0 commit comments