Skip to content

Commit bbd62f4

Browse files
committed
setup instructions; id types
1 parent 6040d28 commit bbd62f4

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

README.md

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,24 @@ config :my_app, Repo,
2121
adapter: Mongo.Ecto,
2222
database: "ecto_simple",
2323
username: "mongodb",
24-
password: "mongosb",
24+
password: "mongodb",
2525
hostname: "localhost"
26+
# Or you can use a connection string (mongodb:// or mongodb+srv://), e.g.:
27+
# mongo_url: "mongodb://mongodb:mongodb@localhost:27017/ecto_simple"
2628

2729
# In your application code
2830
defmodule Repo do
29-
use Ecto.Repo, otp_app: :my_app
31+
use Ecto.Repo,
32+
otp_app: :my_app,
33+
adapter: Mongo.Ecto
34+
35+
def pool() do
36+
Ecto.Adapter.lookup_meta(__MODULE__).pid
37+
end
3038
end
3139

3240
defmodule Weather do
33-
use Ecto.Model
41+
use Ecto.Schema
3442

3543
@primary_key {:id, :binary_id, autogenerate: true}
3644
schema "weather" do
@@ -105,6 +113,18 @@ For additional information on usage please see the documentation for [Ecto](http
105113
| timestamp | `BSON.Timestamp` |
106114
| 64-bit integer | `:integer` |
107115

116+
Primary keys on Ecto schemas are named `:id` and represented as strings, whereas in the actual MongoDB collection, they are named `_id` and stored as object ids. Schema definitions for MongoDB collections should start with `@primary_key {:id, :binary_id, autogenerate: true}` like in the above example. To create an `:id` for an Ecto struct, use `BSON.ObjectId.encode!(Mongo.object_id())`, e.g.:
117+
118+
```elixir
119+
weather = %Weather{
120+
id: BSON.ObjectId.encode!(Mongo.object_id()),
121+
city: "New York City",
122+
temp_lo: 32,
123+
temp_hi: 50,
124+
prcp: 0.0
125+
}
126+
```
127+
108128
Symbols are deprecated by the
109129
[BSON specification](http://bsonspec.org/spec.html). They will be converted
110130
to simple strings on reads. There is no possibility of persisting them to
@@ -126,25 +146,28 @@ The adapter and the driver are tested against most recent versions from 5.0, 6.0
126146

127147
Release 2.0 changes the underlying driver from [`mongodb`](https://github.com/elixir-mongo/mongodb) to [`mongodb_driver`](https://github.com/zookzook/elixir-mongodb-driver) 1.4.0. Calls to the Ecto adapter itself should not require any changes. Some config options are no longer used and can be simply deleted: `pool`, `pool_overflow`, `pool_timeout`.
128148

129-
If you make direct calls to the `Mongo` driver, you will need to update some of them to account for the `mongodb` -> `mongodb_driver` upgrade. Also, remember to replace `:mongodb` with `{:mongodb_driver, "~> 1.4.0"}` in your `mix.exs`. The known updates are:
149+
If you make direct calls to the `Mongo` driver, you will need to update some of them to account for the `mongodb` -> `mongodb_driver` upgrade. Also, remember to replace `:mongodb` with `{:mongodb_driver, "~> 1.4"}` in your `mix.exs`. The known updates are:
150+
130151
1. `Mongo` functions no longer accept a `pool` option or `MyApp.Repo.Pool` module argument. Instead, a pool PID is expected:
131-
```elixir
132-
# Old driver call
133-
Mongo.find(MyApp.Repo.Pool, "my_coll", %{"id": id}, projection: %{"field": 1}, pool: db_pool())
134-
135-
# New driver call
136-
Mongo.find(MyApp.Repo.pool(), "my_coll", %{"id": id}, projection: %{"field": 1})
137-
138-
# repo.ex
139-
# Provided the following function is defined in MyApp.Repo:
140-
defmodule MyApp.Repo do
141-
use Ecto.Repo, otp_app: :my_app, adapter: Mongo.Ecto
142-
143-
def pool() do
144-
Ecto.Adapter.lookup_meta(__MODULE__).pid
145-
end
146-
end
147-
```
152+
153+
```elixir
154+
# Old driver call
155+
Mongo.find(MyApp.Repo.Pool, "my_coll", %{"id": id}, projection: %{"field": 1}, pool: db_pool())
156+
157+
# New driver call
158+
Mongo.find(MyApp.Repo.pool(), "my_coll", %{"id": id}, projection: %{"field": 1})
159+
160+
# repo.ex
161+
# Provided the following function is defined in MyApp.Repo:
162+
defmodule MyApp.Repo do
163+
use Ecto.Repo, otp_app: :my_app, adapter: Mongo.Ecto
164+
165+
def pool() do
166+
Ecto.Adapter.lookup_meta(__MODULE__).pid
167+
end
168+
end
169+
```
170+
148171
2. [`Mongo.command`](https://hexdocs.pm/mongodb_driver/1.4.1/Mongo.html#command/3) requires a keyword list instead of a document. E.g., instead of `Mongo.command(MyApp.Repo.pool(), %{listCollections: 1}, opts)`, do `Mongo.command(MyApp.Repo.pool(), [listCollections: 1], opts)`.
149172
3. `Mongo.ReadPreferences.defaults` is renamed to `Mongo.ReadPreference.merge_defaults`.
150173
4. When passing a `hint` to `Mongo.find_one` etc., if the hinted index does not exist, an error is now returned.

lib/mongo_ecto.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ defmodule Mongo.Ecto do
2828
password: "mongodb",
2929
hostname: "localhost"
3030
31+
For more connection options, see mongodb-driver's [Mongo.start_link](https://hexdocs.pm/mongodb_driver/1.5.2/Mongo.html#start_link/1). Note that to use a connection string (mongodb:// or mongodb+srv://), you must set `mongo_url: ` instead of `url: `.
32+
3133
Each repository in Ecto defines a `start_link/0` function that needs to be
3234
invoked before using the repository. This function is generally from your
3335
supervision tree:

0 commit comments

Comments
 (0)