Skip to content

Commit be592e3

Browse files
authored
Update note on data representation (#205)
1 parent fd1607f commit be592e3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,17 @@ Mongo.insert_many(top, "users", [
135135

136136
## Data Representation
137137

138-
Since BSON documents are ordered Elixir maps cannot be used to fully represent them. This driver chose to accept both maps and lists of key-value pairs when encoding but will only decode documents to lists. This has the side-effect that it's impossible to discern empty arrays from empty documents. Additionally, the driver will accept both atoms and strings for document keys but will only decode to strings. BSON symbols can only be decoded.
138+
This driver chooses to accept both maps and lists of key-value tuples when encoding BSON documents (1), but will only decode documents into maps. This has the side effect that document field order is lost when decoding. Maps are convenient to work with, but map keys are not ordered, unlike BSON document fields.
139+
140+
Driver users should represent documents using a list of tuples when field order matters, for example when sorting by multiple fields:
141+
142+
```elixir
143+
Mongo.find(top, "users", %{}, sort: [last_name: 1, first_name: 1, _id: 1])
144+
```
145+
146+
Additionally, the driver accepts both atoms and strings for document keys, but will only decode them into strings. Creating atoms from arbitrary input (such as database documents) is [discouraged](https://elixir-lang.org/getting-started/mix-otp/genserver.html#:~:text=However%2C%20naming%20dynamic,our%20system%20memory!) because atoms are not garbage collected.
147+
148+
[BSON symbols (deprecated)](https://bsonspec.org/spec.html#:~:text=Symbol.%20%E2%80%94%20Deprecated) can only be decoded (2).
139149

140150
BSON Elixir
141151
---------- ------

0 commit comments

Comments
 (0)