@@ -60,32 +60,26 @@ Then run `mix deps.get` to fetch dependencies.
60
60
61
61
``` elixir
62
62
# 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" )
64
64
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" , %{})
69
67
|> Enum .to_list ()
70
- |> IO .inspect
71
68
```
72
69
73
70
To specify a username and password, use the ` :username ` , ` :password ` , and ` :auth_source ` options.
74
71
75
72
``` elixir
76
73
# Starts an unpooled connection
77
- {:ok , conn } =
74
+ {:ok , top } =
78
75
Mongo .start_link (url: " mongodb://localhost:27017/db-name" ,
79
76
username: " test_user" ,
80
77
password: " hunter2" ,
81
78
auth_source: " admin_test" )
82
79
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" , %{})
87
82
|> Enum .to_list ()
88
- |> IO .inspect
89
83
```
90
84
91
85
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
585
579
the ` compressors=zstd ` to the URL connection string:
586
580
587
581
``` 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" )
589
583
```
590
584
591
585
The driver uses compression for the following functions:
@@ -607,7 +601,7 @@ The driver uses compression for the following functions:
607
601
You can disable the compression for a single function by using the option ` compression: false ` , for example:
608
602
609
603
```
610
- Mongo.find(conn , "tasks", %{}, compression: false)
604
+ Mongo.find(top , "tasks", %{}, compression: false) |> Enum.to_list( )
611
605
```
612
606
The compression significantly reduces the amount of data, while increasing the load on the CPU.
613
607
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
620
614
Simple experiments can be carried out here to determine which size shortens the duration of the queries:
621
615
622
616
``` 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 )
624
618
```
625
619
626
620
## Connection Pooling
@@ -631,32 +625,30 @@ function calls in `Mongo` using the pool:
631
625
632
626
``` elixir
633
627
# 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 )
638
629
639
- cursor
630
+ # Gets an enumerable stream for the results
631
+ top
632
+ |> Mongo .find (" test-collection" , %{})
640
633
|> Enum .to_list ()
641
- |> IO .inspect
642
634
```
643
635
644
636
If you're using pooling it is recommended to add it to your application supervisor:
645
637
646
638
``` elixir
647
639
def start (_type , _args ) do
648
- import Supervisor .Spec
649
640
650
641
children = [
651
- worker ( Mongo , [[ name: :mongo , database : " test" , pool_size: 3 ]])
642
+ { Mongo , [name: :mongo_db , url : " mongodb://localhost:27017/ test" , pool_size: 3 ]}
652
643
]
653
644
654
645
opts = [strategy: :one_for_one , name: MyApp .Supervisor ]
655
646
Supervisor .start_link (children, opts)
656
647
end
657
648
```
658
649
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.
660
652
661
653
## Replica Sets
662
654
@@ -1019,16 +1011,16 @@ for reading from change streams:
1019
1011
``` elixir
1020
1012
seeds = [" hostname1.net:27017" , " hostname2.net:27017" , " hostname3.net:27017" ]
1021
1013
{: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 )
1024
1016
```
1025
1017
1026
1018
An example with a spawned process that sends messages to the monitor process:
1027
1019
1028
1020
``` elixir
1029
1021
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 )
1032
1024
end
1033
1025
1034
1026
spawn (fn -> for_ever (top, self ()) end )
@@ -1088,7 +1080,7 @@ bulk = "bulk"
1088
1080
|> OrderedBulk .delete_one (%{kind: " cat" })
1089
1081
|> OrderedBulk .delete_one (%{kind: " cat" })
1090
1082
1091
- result = Mongo .BulkWrite .write (@topology , bulk, w: 1 )
1083
+ result = Mongo .BulkWrite .write (top , bulk, w: 1 )
1092
1084
```
1093
1085
1094
1086
In the following example we import 1.000.000 integers into the MongoDB using the stream api:
@@ -1219,9 +1211,9 @@ to `Logger.info`:
1219
1211
1220
1212
``` elixir
1221
1213
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" )
1223
1215
{:ok , # PID<0.226.0>}
1224
- iex> Mongo .find_one (conn , " test" , %{})
1216
+ iex> Mongo .find_one (top , " test" , %{}) |> Enum . to_list ( )
1225
1217
[info] Received command: %Mongo .Events .CommandStartedEvent {command: [find: " test" , .. .
1226
1218
[info] Received command: %Mongo .Events .CommandSucceededEvent {command_name: :find , .. .
1227
1219
```
0 commit comments