View Source Mongo.Repo behaviour (mongodb-driver v1.5.0)

Defines a repository.

A repository serves as a convenience module for a mongodb instance.

To include the Mongo.Repo module in your application, you can put the use macro in your app's Repo module.

defmodule MyApp.Repo do
  use Mongo.Repo,
    otp_app: :my_app,
    topology: :mongo
end

With that in place we can configure the Repo:

config :my_app, MyApp.Repo,
  url: "mongodb://localhost:27017/my-app-dev",
  timeout: 60_000,
  idle_interval: 10_000,
  queue_target: 5_000

For a complete list of configuration options take a look at Mongo.

Finally we can add the Mongo.Repo instance to our application supervision tree

children = [
  # ...
  MyApp.Repo,
  # ...
]

Read-only repositories

To explicitly set a repository as read-only, we can pass in the :read_only flag to use:

use Mongo.Repo,
  otp_app: :my_app,
  topology: :mongo,
  read_only: true

The read-only option will not include any write operation related functions in the module.

Summary

Callbacks

Performs aggregation operation using the aggregation pipeline on the given collection module and returns a list of collection structs.

Selects documents for the collection defined in the given module and returns a list of collection structs for the given filter

Returns the mongo configuration stored in the :otp_app environment.

Returns the count of documents in the given collection module for the given filter.

Deletes a document struct from the database and returns a {:ok, doc} tuple.

Same as delete/2 but raises an error.

Deletes all documents for the given collection module and filter.

Checks whether there are any documents in the given collection module for the given filter.

Returns a single document struct for the collection defined in the given module and bson object id as a tuple of {:ok, document}.

Returns a single document struct for the collection defined in the given module and query as a tuple of {:ok, document}.

Returns a single document struct for the collection defined in the given module and bson object id.

Returns a single document struct for the collection defined in the given module and query.

Inserts a new document struct into the database and returns a {:ok, doc} tuple.

Same as insert/2 but raises an error.

Inserts all given documents into the document in one write operation and returns an :ok tuple with the count of inserted documents as second element and the inserted ids as third element.

Upserts a document struct and returns a {:ok, doc} tuple.

Same as insert_or_update/2 but raises an error.

Selects documents for the collection defined in the given module and returns a stream of collection structs for the given filter

Convenient function for running multiple write commands in a transaction.

Updates a document struct and returns a {:ok, doc} tuple.

Same as update/2 but raises an error.

Applies the updates for the documents in the given collection module and filter.

Types

Callbacks

Link to this callback

aggregate(module, pipeline, opts)

View Source (optional)
@callback aggregate(module :: module(), pipeline :: BSON.document(), opts :: Keyword.t()) ::
  [Mongo.Collection.t()] | {:error, any()}

Performs aggregation operation using the aggregation pipeline on the given collection module and returns a list of collection structs.

For all options see Options

Example

MyApp.Repo.aggregate(Post, [
  %{"$match" => %{title: title}},
  %{"$sort" => [{"title", -1}]},
  %{"$limit" => 10}
])
Link to this callback

all(module, filter, opts)

View Source (optional)
@callback all(module :: module(), filter :: BSON.document(), opts :: Keyword.t()) ::
  [Mongo.Collection.t()] | {:error, any()}

Selects documents for the collection defined in the given module and returns a list of collection structs for the given filter

For all options see Options

Example

MyApp.Repo.all(Post, %{title: title})
MyApp.Repo.all(Post, %{title: title}, batch_size: 2)
@callback config() :: Keyword.t()

Returns the mongo configuration stored in the :otp_app environment.

Link to this callback

count(module, filter, opts)

View Source
@callback count(module :: module(), filter :: BSON.document(), opts :: Keyword.t()) ::
  {:ok, integer()}

Returns the count of documents in the given collection module for the given filter.

Options

  • :limit - Maximum number of documents to fetch with the cursor
  • :skip - Number of documents to skip before returning the first

Example

MyApp.Repo.count(Post)
Link to this callback

delete(doc, opts)

View Source (optional)
@callback delete(doc :: Mongo.Collection.t(), opts :: Keyword.t()) ::
  {:ok, Mongo.Collection.t()} | {:error, any()}

Deletes a document struct from the database and returns a {:ok, doc} tuple.

For all options see Options

Example

MyApp.Repo.delete(post)
Link to this callback

delete!(doc, opts)

View Source (optional)
@callback delete!(doc :: Mongo.Collection.t(), opts :: Keyword.t()) ::
  Mongo.Collection.t()

Same as delete/2 but raises an error.

Link to this callback

delete_all(module, filter, opts)

View Source (optional)
@callback delete_all(module :: module(), filter :: BSON.document(), opts :: Keyword.t()) ::
  {:ok, Mongo.DeleteResult.t()}

Deletes all documents for the given collection module and filter.

For all options see Options

Example

MyApp.Repo.delete_all(Post, %{})
MyApp.Repo.delete_all(Post, %{title: "todelete"})
Link to this callback

exists?(module, filter, opts)

View Source (optional)
@callback exists?(module :: module(), filter :: BSON.document(), opts :: Keyword.t()) ::
  boolean()

Checks whether there are any documents in the given collection module for the given filter.

Returns a boolean.

Example

MyApp.Repo.exists?(Post, %{title: title})
Link to this callback

fetch(module, id, opts)

View Source (optional)
@callback fetch(module :: module(), id :: BSON.ObjectId.t(), opts :: Keyword.t()) ::
  {:ok, Mongo.Collection.t()} | {:error, :not_found} | {:error, any()}

Returns a single document struct for the collection defined in the given module and bson object id as a tuple of {:ok, document}.

Returns {:error, :not_found} if no result was found.

If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk.

For all options see Options

Example

MyApp.Repo.fetch(Post, id)
MyApp.Repo.fetch(Post, id, read_concern: %{level: "local"})
Link to this callback

fetch_by(module, query, opts)

View Source (optional)
@callback fetch_by(module :: module(), query :: BSON.document(), opts :: Keyword.t()) ::
  {:ok, Mongo.Collection.t()} | {:error, :not_found} | {:error, any()}

Returns a single document struct for the collection defined in the given module and query as a tuple of {:ok, document}.

Returns {:error, :not_found} if no result was found.

If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk.

For all options see Options

Example

MyApp.Repo.fetch_by(Post, %{title: title})
MyApp.Repo.fetch_by(Post, %{title: title}, read_concern: %{level: "local"})
Link to this callback

get(module, id, opts)

View Source (optional)
@callback get(module :: module(), id :: BSON.ObjectId.t(), opts :: Keyword.t()) ::
  Mongo.Collection.t() | nil | {:error, any()}

Returns a single document struct for the collection defined in the given module and bson object id.

Returns nil if no result was found.

If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk.

For all options see Options

Example

MyApp.Repo.get(Post, id)
MyApp.Repo.get(Post, id, read_concern: %{level: "local"})
Link to this callback

get_by(module, query, opts)

View Source (optional)
@callback get_by(module :: module(), query :: BSON.document(), opts :: Keyword.t()) ::
  Mongo.Collection.t() | nil | {:error, any()}

Returns a single document struct for the collection defined in the given module and query.

Returns nil if no result was found.

If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk.

For all options see Options

Example

MyApp.Repo.get_by(Post, %{title: title})
MyApp.Repo.get_by(Post, %{title: title}, read_concern: %{level: "local"})
Link to this callback

insert(doc, opts)

View Source (optional)
@callback insert(doc :: Mongo.Collection.t(), opts :: Keyword.t()) ::
  {:ok, Mongo.Collection.t()} | {:error, any()}

Inserts a new document struct into the database and returns a {:ok, doc} tuple.

For all options see Options

Example

MyApp.Repo.insert(Post.new())
Link to this callback

insert!(doc, opts)

View Source (optional)
@callback insert!(doc :: Mongo.Collection.t(), opts :: Keyword.t()) ::
  Mongo.Collection.t()

Same as insert/2 but raises an error.

Link to this callback

insert_all(module, entries, opts)

View Source (optional)
@callback insert_all(
  module :: module(),
  entries :: [BSON.document()],
  opts :: Keyword.t()
) ::
  {:ok, integer(), [BSON.ObjectId.t()]} | {:error, any()}

Inserts all given documents into the document in one write operation and returns an :ok tuple with the count of inserted documents as second element and the inserted ids as third element.

Returns {:error, reason} on failure.

Example

MyApp.Repo.insert_all(Post, [%{title: "a"}, %{title: "b"}])
Link to this callback

insert_or_update(doc, opts)

View Source (optional)
@callback insert_or_update(doc :: Mongo.Collection.t(), opts :: Keyword.t()) ::
  {:ok, Mongo.Collection.t()} | {:error, any()}

Upserts a document struct and returns a {:ok, doc} tuple.

Example

MyApp.Repo.insert_or_update(Post.new())
Link to this callback

insert_or_update!(doc, opts)

View Source (optional)
@callback insert_or_update!(doc :: Mongo.Collection.t(), opts :: Keyword.t()) ::
  Mongo.Collection.t()

Same as insert_or_update/2 but raises an error.

Link to this callback

stream(module, filter, opts)

View Source (optional)
@callback stream(module :: module(), filter :: BSON.document(), opts :: Keyword.t()) ::
  Enumerable.t() | {:error, any()}

Selects documents for the collection defined in the given module and returns a stream of collection structs for the given filter

For all options see Options

Example

MyApp.Repo.stream(Post, %{title: title})
MyApp.Repo.stream(Post, %{title: title}, batch_size: 2)
@callback transaction(fun :: (... -> {:ok, any()} | :error), opts :: Keyword.t()) ::
  {:ok, any()} | :error | {:error, any()}

Convenient function for running multiple write commands in a transaction.

In case of TransientTransactionError or UnknownTransactionCommitResult the function will retry the whole transaction or the commit of the transaction. You can specify a timeout (:transaction_retry_timeout_s) to limit the time of repeating. The default value is 120 seconds. If you don't wait so long, you call transaction with the option transaction_retry_timeout_s: 10. In this case after 10 seconds of retrying, the function will return an error.

You can nest the function calls. In this case the first session will be reused.

Link to this callback

update(doc, opts)

View Source (optional)
@callback update(doc :: Mongo.Collection.t(), opts :: Keyword.t()) ::
  {:ok, Mongo.Collection.t()} | {:error, any()}

Updates a document struct and returns a {:ok, doc} tuple.

Example

post = MyApp.Repo.insert!(Post.new())
MyApp.Repo.update(%{post | title: "new"})
Link to this callback

update!(doc, opts)

View Source (optional)
@callback update!(doc :: Mongo.Collection.t(), opts :: Keyword.t()) ::
  Mongo.Collection.t()

Same as update/2 but raises an error.

Link to this callback

update_all(module, filter, update, opts)

View Source (optional)
@callback update_all(
  module :: module(),
  filter :: BSON.document(),
  update :: BSON.document(),
  opts :: Keyword.t()
) :: {:ok, Mongo.result(Mongo.UpdateResult.t())}

Applies the updates for the documents in the given collection module and filter.

Uses MongoDB update operators to specify the updates. For more information and all options please refer to the MongoDB documentation

Example

MyApp.Repo.update_all(Post, %{}, %{"$set" => %{title: "updated"}})
MyApp.Repo.update_all(Post, %{title: "old"}, %{"$set" => %{title: "updated"}})