Skip to content

Commit 3edb0f3

Browse files
committed
Merge branch 'master' into OP_MSG
# Conflicts: # lib/bson/decoder.ex # lib/mongo/messages.ex # lib/mongo/mongo_db_connection.ex # lib/mongo/query.ex # lib/mongo_db_connection/utils.ex
2 parents 8d978bb + 016fc16 commit 3edb0f3

22 files changed

+416
-601
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ before_script:
2929

3030
script:
3131
- mix test
32-
- not_allowed=(1.3 1.4 1.5)
32+
- not_allowed=(1.3 1.4 1.5 1.6 1.7)
3333
- case "${not_allowed[@]}" in *"${TRAVIS_ELIXIR_VERSION}"*) dialyzer=0 ;; *) dialyzer=1 ;; esac
3434
- if [ "$dialyzer" -eq 1 ]; then mix dialyzer --halt-exit-status; fi
3535

3636
env:
3737
matrix:
38-
- MONGOVERSION=3.0.15 TRAVIS_NODE_VERSION=4
3938
- MONGOVERSION=3.2.22 TRAVIS_NODE_VERSION=4
4039
- MONGOVERSION=3.4.18 TRAVIS_NODE_VERSION=4
4140
- MONGOVERSION=3.6.9 TRAVIS_NODE_VERSION=4
42-
- MONGOVERSION=4.0.5 TRAVIS_NODE_VERSION=4
41+
- MONGOVERSION=4.0.8 TRAVIS_NODE_VERSION=4

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## v0.5.0-dev
2+
3+
* Enhancements
4+
* upgraded to DBConnection 2.0.6
5+
* refactored code, simplified code and api
6+
* replaced deprecated op_code by database commands
7+
* update_one, update_many, replace_one, replace_many return upserted ids
8+
* support for all find options
9+
110
## v0.4.8-dev
211

312
* Enhancements

README.md

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# Mongodb
1+
# An alternative Mongodb driver for Elixir
2+
[![Build Status](https://travis-ci.org/zookzook/elixir-mongodb-driver.svg?branch=master)](https://travis-ci.org/zookzook/elixir-mongodb-driver)
3+
[![Hex.pm](https://img.shields.io/hexpm/v/mongodb_driver.svg)](https://hex.pm/packages/mongodb_driver)
4+
[![Hex.pm](https://img.shields.io/hexpm/dt/mongodb_driver.svg)](https://hex.pm/packages/mongodb_driver)
5+
[![Hex.pm](https://img.shields.io/hexpm/dw/mongodb_driver.svg)](https://hex.pm/packages/mongodb_driver)
6+
[![Hex.pm](https://img.shields.io/hexpm/dd/mongodb_driver.svg)](https://hex.pm/packages/mongodb_driver)
27

38
This is an alternative development from the [original](https://github.com/ankhers/mongodb), which was the starting point
49
and already contained very nice code.
@@ -9,19 +14,22 @@ replaced some op code calls with command calls.
914
This is currently under development and there is no version management yet. The main goal is to simplify the API and
1015
to implement the current requirements for the driver.
1116

17+
[Documentation](https://hexdocs.pm/mongodb_driver/readme.html)
18+
1219
## Motivation
1320

14-
* Refactoring old code into new
15-
* Understand the magic in the code
16-
* Simplify code: remove raw_find (raw_find called from cursors, raw_find called with "$cmd"), so raw_find is more calling a command than a find query.
17-
* Better support for new MongoDB version, for example the ability to use views
18-
* Upgrade to DBConnection 2.x
19-
* Because the driver is used in production environments, quick adjustments are necessary.
21+
* [ ] Refactoring old code into new
22+
* [ ] Understand the magic in the code
23+
* [x] Simplify code: remove raw_find (raw_find called from cursors, raw_find called with "$cmd"), so raw_find is more calling a command than a find query.
24+
* [x] Better support for new MongoDB version, for example the ability to use views
25+
* [x] Upgrade to ([DBConnection 2.x](https://github.com/elixir-ecto/db_connection))
26+
* [x] Removed depreacated op codes ([See](https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#request-opcodes))
27+
* [ ] Because the driver is used in production environments, quick adjustments are necessary.
2028

2129
## Features
2230

23-
* Supports MongoDB versions 3.0, 3.2, 3.4, 3.6, 4.0
24-
* Connection pooling (through db_connection)
31+
* Supports MongoDB versions 3.2, 3.4, 3.6, 4.0
32+
* Connection pooling ([through DBConnection 2.x](https://github.com/elixir-ecto/db_connection))
2533
* Streaming cursors
2634
* Performant ObjectID generation
2735
* Follows driver specification set by 10gen
@@ -57,25 +65,20 @@ to implement the current requirements for the driver.
5765

5866
### Installation:
5967

60-
Add mongodb to your mix.exs `deps` and `:applications` (replace `>= 0.0.0` in `deps` if you want a specific version). Mongodb supports the same pooling libraries db_connection does (currently: no pooling, poolboy, and sbroker). If you want to use poolboy as pooling library you should set up your project like this:
68+
Add `mongodb_driver` to your mix.exs `deps` and `:applications`.
6169

6270
```elixir
6371
def application do
64-
[applications: [:mongodb, :poolboy]]
72+
[applications: [:mongodb_driver]]
6573
end
6674

6775
defp deps do
68-
[{:mongodb, ">= 0.0.0"},
69-
{:poolboy, ">= 0.0.0"}]
76+
[{:mongodb_driver, "~> 0.5.0"}]
7077
end
7178
```
7279

7380
Then run `mix deps.get` to fetch dependencies.
7481

75-
### Connection pooling
76-
77-
By default mongodb will start a single connection, but it also supports pooling with the `:pool` option. For poolboy add the `pool: DBConnection.Poolboy` option to `Mongo.start_link` and to all function calls in `Mongo` using the pool.
78-
7982
```elixir
8083
# Starts an unpooled connection
8184
{:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/db-name")
@@ -88,46 +91,56 @@ cursor
8891
|> IO.inspect
8992
```
9093

94+
### Connection pooling
95+
The driver supports pooling by DBConnection (2.x). By default `mongodb_driver` will start a single
96+
connection, but it also supports pooling with the `:pool_size` option. For 3 connections add the `pool_size: 3` option to `Mongo.start_link` and to all
97+
function calls in `Mongo` using the pool:
98+
99+
```elixir
100+
# Starts an pooled connection
101+
{:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/db-name", pool_size: 3)
102+
103+
# Gets an enumerable cursor for the results
104+
cursor = Mongo.find(conn, "test-collection", %{})
105+
106+
cursor
107+
|> Enum.to_list()
108+
|> IO.inspect
109+
```
110+
91111
If you're using pooling it is recommend to add it to your application supervisor:
92112

93113
```elixir
94114
def start(_type, _args) do
95115
import Supervisor.Spec
96116

97117
children = [
98-
worker(Mongo, [[name: :mongo, database: "test", pool: DBConnection.Poolboy]])
118+
worker(Mongo, [[name: :mongo, database: "test", pool_size: 3]])
99119
]
100120

101121
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
102122
Supervisor.start_link(children, opts)
103123
end
104124
```
105125

106-
DBConnection.Poolboy defaults to [10 Poolboy connections](https://hexdocs.pm/db_connection/1.1.3/DBConnection.Poolboy.html#content), but you can change that with the `:pool_size` option:
107-
```elixir
108-
{:ok, conn} = Mongo.start_link(name: :mongo, database: "test", pool: DBConnection.Poolboy, pool_size: 2)
109-
```
110-
111-
112-
Remember to specify the pool in each query. There is [some discussion](https://github.com/ankhers/mongodb/issues/175) on how to change this requirement.
113-
114-
```elixir
115-
Mongo.find(:mongo, "collection", %{}, limit: 20, pool: DBConnection.Poolboy)
116-
```
126+
Due to the mongodb specification, an additional connection is always set up for the monitor process.
117127

118128
### Replica Sets
119129

120-
To connect to a Mongo cluster that is using replica sets, it is recommended to use the `:seeds` list instead of a `:hostname` and `:port` pair.
130+
To connect to a Mongo cluster that is using replica sets, it is recommended to use the `:seeds` list instead
131+
of a `:hostname` and `:port` pair.
121132

122133
```elixir
123134
{:ok, pid} = Mongo.start_link(database: "test", seeds: ["hostname1.net:27017", "hostname2.net:27017"])
124135
```
125136

126-
This will allow for scenarios where the first `"hostname1.net:27017"` is unreachable for any reason and will automatically try to connect to each of the following entries in the list to connect to the cluster.
137+
This will allow for scenarios where the first `"hostname1.net:27017"` is unreachable for any reason
138+
and will automatically try to connect to each of the following entries in the list to connect to the cluster.
127139

128140
### Auth mechanisms
129141

130-
For versions of Mongo 3.0 and greater, the auth mechanism defaults to SCRAM. If you'd like to use [MONGODB-X509](https://docs.mongodb.com/manual/tutorial/configure-x509-client-authentication/#authenticate-with-a-x-509-certificate)
142+
For versions of Mongo 3.0 and greater, the auth mechanism defaults to SCRAM.
143+
If you'd like to use [MONGODB-X509](https://docs.mongodb.com/manual/tutorial/configure-x509-client-authentication/#authenticate-with-a-x-509-certificate)
131144
authentication, you can specify that as a `start_link` option.
132145

133146
```elixir
@@ -136,7 +149,8 @@ authentication, you can specify that as a `start_link` option.
136149

137150
### AWS, TLS and Erlang SSL ciphers
138151

139-
Some MongoDB cloud providers (notably AWS) require a particular TLS cipher that isn't enabled by default in the Erlang SSL module. In order to connect to these services,
152+
Some MongoDB cloud providers (notably AWS) require a particular TLS cipher that isn't enabled
153+
by default in the Erlang SSL module. In order to connect to these services,
140154
you'll want to add this cipher to your `ssl_opts`:
141155

142156
```elixir
@@ -191,7 +205,10 @@ $ mongod --sslMode allowSSL --sslPEMKeyFile /path/to/mongodb.pem
191205

192206
## License
193207

194-
Copyright 2015 Justin Wood
208+
Copyright 2015 Eric Meadows-Jönsson and Justin Wood
209+
210+
Copyright 2019 Michael Maier
211+
195212

196213
Licensed under the Apache License, Version 2.0 (the "License");
197214
you may not use this file except in compliance with the License.

lib/bson/decoder.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ defmodule BSON.Decoder do
88
map
99
end
1010

11-
def documents(binary),
12-
do: documents(binary, [])
13-
def documents("", acc),
14-
do: Enum.reverse(acc)
11+
def documents(binary), do: documents(binary, [])
12+
def documents("", acc), do: Enum.reverse(acc)
1513
def documents(binary, acc) do
1614
{doc, rest} = document(binary)
1715
documents(rest, [doc|acc])
@@ -174,6 +172,6 @@ defmodule BSON.Decoder do
174172
defp subtype(0x02), do: :binary_old
175173
defp subtype(0x03), do: :uuid_old
176174
defp subtype(0x04), do: :uuid
177-
defp subtype(0x05), do: :md5
175+
defp subtype(0x05), do: :md5
178176
defp subtype(int) when is_integer(int) and int in 0x80..0xFF, do: int
179177
end

0 commit comments

Comments
 (0)