Skip to content

Commit abb3b7d

Browse files
committed
chore: updated the code examples
1 parent 86a20a8 commit abb3b7d

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

README.md

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,26 @@ Then run `mix deps.get` to fetch dependencies.
6060

6161
```elixir
6262
# Starts an unpooled connection
63-
{:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/my-database")
63+
{:ok, top} = Mongo.start_link(url: "mongodb://localhost:27017/my-database")
6464

65-
# Gets an enumerable cursor for the results
66-
cursor = Mongo.find(conn, "test-collection", %{})
67-
68-
cursor
65+
top
66+
|> Mongo.find("test-collection", %{})
6967
|> Enum.to_list()
70-
|> IO.inspect
7168
```
7269

7370
To specify a username and password, use the `:username`, `:password`, and `:auth_source` options.
7471

7572
```elixir
7673
# Starts an unpooled connection
77-
{:ok, conn} =
74+
{:ok, top} =
7875
Mongo.start_link(url: "mongodb://localhost:27017/db-name",
7976
username: "test_user",
8077
password: "hunter2",
8178
auth_source: "admin_test")
8279

83-
# Gets an enumerable cursor for the results
84-
cursor = Mongo.find(conn, "test-collection", %{})
85-
86-
cursor
80+
top
81+
|> Mongo.find("test-collection", %{})
8782
|> Enum.to_list()
88-
|> IO.inspect
8983
```
9084

9185
For secure requests, you may need to add some more options; see the "AWS, TLS and Erlang SSL ciphers" section below.
@@ -585,7 +579,7 @@ The driver will provide the related code. After activating the zstd compressor c
585579
the `compressors=zstd` to the URL connection string:
586580

587581
```elixir
588-
{:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/my_database?compressors=zstd&maxPoolSize=10")
582+
{:ok, top} = Mongo.start_link(url: "mongodb://localhost:27017/my_database?compressors=zstd&maxPoolSize=10")
589583
```
590584

591585
The driver uses compression for the following functions:
@@ -607,7 +601,7 @@ The driver uses compression for the following functions:
607601
You can disable the compression for a single function by using the option `compression: false`, for example:
608602

609603
```
610-
Mongo.find(conn, "tasks", %{}, compression: false)
604+
Mongo.find(top, "tasks", %{}, compression: false) |> Enum.to_list()
611605
```
612606
The compression significantly reduces the amount of data, while increasing the load on the CPU.
613607
This is certainly interesting for environments in which network transmission has to be paid for.
@@ -620,7 +614,7 @@ The speed also depends on the `batch_size` attribute. A higher speed is achieved
620614
Simple experiments can be carried out here to determine which size shortens the duration of the queries:
621615

622616
```elixir
623-
:timer.tc(fn -> Mongo.find(conn, "tasks", %{}, limit: 30_000, batch_size: 1000) |> Stream.reject(fn _x -> true end) |> Stream.run() end)
617+
:timer.tc(fn -> Mongo.find(top, "tasks", %{}, limit: 30_000, batch_size: 1000) |> Stream.reject(fn _x -> true end) |> Stream.run() end)
624618
```
625619

626620
## Connection Pooling
@@ -631,32 +625,30 @@ function calls in `Mongo` using the pool:
631625

632626
```elixir
633627
# Starts an pooled connection
634-
{:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/db-name", pool_size: 3)
635-
636-
# Gets an enumerable cursor for the results
637-
cursor = Mongo.find(conn, "test-collection", %{})
628+
{:ok, top} = Mongo.start_link(url: "mongodb://localhost:27017/db-name", pool_size: 3)
638629

639-
cursor
630+
# Gets an enumerable stream for the results
631+
top
632+
|> Mongo.find("test-collection", %{})
640633
|> Enum.to_list()
641-
|> IO.inspect
642634
```
643635

644636
If you're using pooling it is recommended to add it to your application supervisor:
645637

646638
```elixir
647639
def start(_type, _args) do
648-
import Supervisor.Spec
649640

650641
children = [
651-
worker(Mongo, [[name: :mongo, database: "test", pool_size: 3]])
642+
{Mongo, [name: :mongo_db, url: "mongodb://localhost:27017/test", pool_size: 3]}
652643
]
653644

654645
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
655646
Supervisor.start_link(children, opts)
656647
end
657648
```
658649

659-
Due to the mongodb specification, an additional connection is always set up for the monitor process.
650+
We can use the `:mongo_db` atom instead of a process pid. This allows us to call the `Mongo` functions directly from
651+
every place in the code.
660652

661653
## Replica Sets
662654

@@ -1019,16 +1011,16 @@ for reading from change streams:
10191011
```elixir
10201012
seeds = ["hostname1.net:27017", "hostname2.net:27017", "hostname3.net:27017"]
10211013
{:ok, top} = Mongo.start_link(database: "my-db", seeds: seeds, appname: "getting rich")
1022-
cursor = Mongo.watch_collection(top, "accounts", [], fn doc -> IO.puts "New Token #{inspect doc}" end, max_time: 2_000 )
1023-
cursor |> Enum.each(fn doc -> IO.puts inspect doc end)
1014+
stream = Mongo.watch_collection(top, "accounts", [], fn doc -> IO.puts "New Token #{inspect doc}" end, max_time: 2_000 )
1015+
Enum.each(stream, fn doc -> IO.puts inspect doc end)
10241016
```
10251017

10261018
An example with a spawned process that sends messages to the monitor process:
10271019

10281020
```elixir
10291021
def for_ever(top, monitor) do
1030-
cursor = Mongo.watch_collection(top, "users", [], fn doc -> send(monitor, {:token, doc}) end)
1031-
cursor |> Enum.each(fn doc -> send(monitor, {:change, doc}) end)
1022+
stream = Mongo.watch_collection(top, "users", [], fn doc -> send(monitor, {:token, doc}) end)
1023+
Enum.each(stream, fn doc -> send(monitor, {:change, doc}) end)
10321024
end
10331025

10341026
spawn(fn -> for_ever(top, self()) end)
@@ -1088,7 +1080,7 @@ bulk = "bulk"
10881080
|> OrderedBulk.delete_one(%{kind: "cat"})
10891081
|> OrderedBulk.delete_one(%{kind: "cat"})
10901082

1091-
result = Mongo.BulkWrite.write(@topology, bulk, w: 1)
1083+
result = Mongo.BulkWrite.write(top, bulk, w: 1)
10921084
```
10931085

10941086
In the following example we import 1.000.000 integers into the MongoDB using the stream api:
@@ -1219,9 +1211,9 @@ to `Logger.info`:
12191211

12201212
```elixir
12211213
iex> Mongo.EventHandler.start()
1222-
iex> {:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/test")
1214+
iex> {:ok, top} = Mongo.start_link(url: "mongodb://localhost:27017/test")
12231215
{:ok, #PID<0.226.0>}
1224-
iex> Mongo.find_one(conn, "test", %{})
1216+
iex> Mongo.find_one(top, "test", %{}) |> Enum.to_list()
12251217
[info] Received command: %Mongo.Events.CommandStartedEvent{command: [find: "test", ...
12261218
[info] Received command: %Mongo.Events.CommandSucceededEvent{command_name: :find, ...
12271219
```

0 commit comments

Comments
 (0)