ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - elixir-mongo/mongodb_ecto: MongoDB adapter for Ecto
MongoDB adapter for Ecto. Contribute to elixir-mongo/mongodb_ecto development by creating an account on GitHub.
Visit Site

GitHub - elixir-mongo/mongodb_ecto: MongoDB adapter for Ecto

GitHub - elixir-mongo/mongodb_ecto: MongoDB adapter for 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

# In your config/config.exs file
config :my_app, Repo,
  adapter: Mongo.Ecto,
  database: "ecto_simple",
  username: "mongodb",
  password: "mongosb",
  hostname: "localhost"

# In your application code
defmodule Repo do
  use Ecto.Repo, otp_app: :my_app
end

defmodule Weather do
  use Ecto.Model

  @primary_key {:id, :binary_id, autogenerate: true}
  schema "weather" do
    field :city     # Defaults to type :string
    field :temp_lo, :integer
    field :temp_hi, :integer
    field :prcp,    :float, default: 0.0
  end
end

defmodule Simple do
  import Ecto.Query

  def sample_query do
    query = from w in Weather,
          where: w.prcp > 0 or is_nil(w.prcp),
         select: w
    Repo.all(query)
  end
end

Usage

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

def deps do
  [
    {:mongodb_ecto, "~> 1.0.0"}
  ]
end

You should also update your applications to include both projects:

def application do
  [applications: [:logger, :mongodb_ecto, :ecto]]
end

To use the adapter in your repo:

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Mongo.Ecto
end

For additional information on usage please see the documentation 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.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.

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:

  1. Mongo functions no longer accept a pool option or MyApp.Repo.Pool module argument. Instead, a pool PID is expected:
    # Old driver call
    Mongo.find(MyApp.Repo.Pool, "my_coll", %{"id": id}, projection: %{"field": 1}, pool: db_pool())
    
    # New driver call
    Mongo.find(MyApp.Repo.pool(), "my_coll", %{"id": id}, projection: %{"field": 1})
    
    # repo.ex
    # Provided the following function is defined in MyApp.Repo:
    defmodule MyApp.Repo do
      use Ecto.Repo, otp_app: :my_app, adapter: Mongo.Ecto
    
      def pool() do
        Ecto.Adapter.lookup_meta(__MODULE__).pid
      end
    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:

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

Copyright and License

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.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory