项目作者: kobil-systems

项目描述 :
MongoDB adapter for Ecto
高级语言: Elixir
项目地址: git://github.com/kobil-systems/mongodb_ecto.git
创建时间: 2015-05-25T19:18:02Z
项目社区:https://github.com/kobil-systems/mongodb_ecto

开源协议:

下载


Mongo.Ecto

CI
Hex.pm
Module Version
Hex Docs
Total Download
License
Last Updated

Mongo.Ecto is a MongoDB adapter for Ecto.

For detailed information read the documentation for the Mongo.Ecto module,
or check out examples below.

Example

  1. # In your config/config.exs file
  2. config :my_app, Repo,
  3. adapter: Mongo.Ecto,
  4. # Example key-value configuration:
  5. database: "ecto_simple",
  6. username: "mongodb",
  7. password: "mongodb",
  8. hostname: "localhost"
  9. # OR you can configure with a connection string (mongodb:// or mongodb+srv://):
  10. mongo_url: "mongodb://mongodb:mongodb@localhost:27017/ecto_simple"
  11. config :my_app,
  12. # Add Repo to this list so you can run commands like `mix ecto.create`.
  13. ecto_repos: [Repo]
  14. # In your application code
  15. defmodule Repo do
  16. use Ecto.Repo,
  17. otp_app: :my_app,
  18. adapter: Mongo.Ecto
  19. def pool() do
  20. Ecto.Adapter.lookup_meta(__MODULE__).pid
  21. end
  22. end
  23. defmodule Weather do
  24. use Ecto.Schema
  25. # see Mongo.Ecto module docs for explanation of this line
  26. @primary_key {:id, :binary_id, autogenerate: true}
  27. # weather is the MongoDB collection name
  28. schema "weather" do
  29. field :city # Defaults to type :string
  30. field :temp_lo, :integer
  31. field :temp_hi, :integer
  32. field :prcp, :float, default: 0.0
  33. end
  34. end
  35. defmodule Simple do
  36. import Ecto.Query
  37. def sample_query do
  38. query = from w in Weather,
  39. where: w.prcp > 0 or is_nil(w.prcp),
  40. select: w
  41. Repo.all(query)
  42. end
  43. end

Usage

Add :mongodb_ecto as a dependency in your mix.exs file.

  1. def deps do
  2. [
  3. {:mongodb_ecto, "~> 2.1.1"}
  4. ]
  5. end

To use the adapter in your repo:

  1. defmodule MyApp.Repo do
  2. use Ecto.Repo,
  3. otp_app: :my_app,
  4. adapter: Mongo.Ecto
  5. end

For additional information on usage please see the documentation for the Mongo.Ecto module and for Ecto.

Data Type Mapping

BSON Ecto
double :float
string :string
object :map
array {:array, subtype}
binary data :binary
binary data (uuid) Ecto.UUID
object id :binary_id
boolean :boolean
date Ecto.DateTime
regular expression Mongo.Ecto.Regex
JavaScript Mongo.Ecto.JavaScript
symbol (see below)
32-bit integer :integer
timestamp BSON.Timestamp
64-bit integer :integer

Symbols are deprecated by the
BSON specification. They will be converted
to simple strings on reads. There is no possibility of persisting them to
the database.

Additionally special values are translated as follows:

BSON Ecto
null nil
min key :BSON_min
max key :BSON_max

Supported Mongo versions

The adapter and the driver are tested against most recent versions from 5.0, 6.0, and 7.0.

Migrating to 2.0

Release 2.0 changes the underlying driver from mongodb to mongodb_driver 1.4. 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.

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:

  1. Mongo functions no longer accept a pool option or MyApp.Repo.Pool module argument. Instead, a pool PID is expected:

    1. # Old driver call
    2. Mongo.find(MyApp.Repo.Pool, "my_coll", %{"id": id}, projection: %{"field": 1}, pool: db_pool())
    3. # New driver call
    4. Mongo.find(MyApp.Repo.pool(), "my_coll", %{"id": id}, projection: %{"field": 1})
    5. # repo.ex
    6. # Provided the following function is defined in MyApp.Repo:
    7. defmodule MyApp.Repo do
    8. use Ecto.Repo, otp_app: :my_app, adapter: Mongo.Ecto
    9. def pool() do
    10. Ecto.Adapter.lookup_meta(__MODULE__).pid
    11. end
    12. end
  2. Mongo.command 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).

  3. Mongo.ReadPreferences.defaults is renamed to Mongo.ReadPreference.merge_defaults.
  4. When passing a hint to Mongo.find_one etc., if the hinted index does not exist, an error is now returned.

Contributing

To contribute you need to compile Mongo.Ecto from source and test it:

  1. $ git clone https://github.com/ankhers/mongodb_ecto.git
  2. $ cd mongodb_ecto
  3. $ mix test

Copyright 2015 Michał Muskała

Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.