MongoX v0.1.2 Mongo

The main entry point for doing queries. All functions take a pool to run the query on.

Read options

All read operations that returns a cursor take the following options for controlling the behaviour of the cursor.

  • :batch_size - Number of documents to fetch in each batch
  • :limit - Maximum number of documents to fetch with the cursor

Write options

All write operations take the following options for controlling the write concern.

  • :w - The number of servers to replicate to before returning from write operators, a 0 value will return immediately, :majority will wait until the operation propagates to a majority of members in the replica set (Default: 1)
  • :j If true, the write operation will only return after it has been committed to journal - (Default: false)
  • :wtimeout - If the write concern is not satisfied in the specified interval, the operation returns an error

Logging

All operations take a boolean log option, that determines, whether the pool’s log/5 function will be called.

Summary

Functions

Performs aggregation operation using the aggregation pipeline

Returns the count of documents that would match a find/4 query

Remove all documents matching the filter from the collection

Remove a document matching the filter from the collection

Finds the distinct values for a specified field across a collection

Selects documents in a collection and returns a cursor for the selected documents

Insert multiple documents into the collection

Insert a single document into the collection

Replace a single document matching the filter with the new document

Issue a database command. If the command has parameters use a keyword list for the document because the “command key” has to be the first in the document

Updates documents or inserts them

Updates an existing document or inserts a new one

Update all documents matching the filter

Update a single document matching the filter

Types

Functions

aggregate(pool, coll, pipeline, opts \\ [])

Performs aggregation operation using the aggregation pipeline.

Options

  • :allow_disk_use - Enables writing to temporary files (Default: false)
  • :max_time - Specifies a time limit in milliseconds
  • :use_cursor - Use a cursor for a batched response (Default: true)
count(pool, coll, filter, opts \\ [])

Specs

count(Mongo.Pool.t, collection, BSON.document, Keyword.t) :: non_neg_integer

Returns the count of documents that would match a find/4 query.

Options

  • :limit - Maximum number of documents to fetch with the cursor
  • :skip - Number of documents to skip before returning the first
  • :hint - Hint which index to use for the query
delete_many(pool, coll, filter, opts \\ [])

Specs

Remove all documents matching the filter from the collection.

delete_one(pool, coll, filter, opts \\ [])

Specs

Remove a document matching the filter from the collection.

distinct(pool, coll, field, filter, opts \\ [])

Specs

Finds the distinct values for a specified field across a collection.

Options

  • :max_time - Specifies a time limit in milliseconds
find(pool, coll, filter, opts \\ [])

Selects documents in a collection and returns a cursor for the selected documents.

Options

  • :comment - Associates a comment to a query
  • :cursor_type - Set to :tailable or :tailable_await to return a tailable cursor
  • :max_time - Specifies a time limit in milliseconds
  • :modifiers - Meta-operators modifying the output or behavior of a query, see http://docs.mongodb.org/manual/reference/operator/query-modifier/
  • :cursor_timeout - Set to false if cursor should not close after 10 minutes (Default: true)
  • :order_by - Sorts the results of a query in ascending or descending order
  • :projection - Limits the fields to return for all matching document
  • :skip - The number of documents to skip before returning (Default: 0)
insert_many(pool, coll, docs, opts \\ [])

Specs

Insert multiple documents into the collection.

If any of the documents is missing the _id field or it is nil, an ObjectId will be generated, and insertd into the document. Ids of all documents will be returned in the result struct.

Options

  • :continue_on_error - even if insert fails for one of the documents continue inserting the remaining ones (default: false)
insert_one(pool, coll, doc, opts \\ [])

Insert a single document into the collection.

If the document is missing the _id field or it is nil, an ObjectId will be generated, inserted into the document, and returned in the result struct.

replace_one(pool, coll, filter, replacement, opts \\ [])

Replace a single document matching the filter with the new document.

Options

  • :upsert - if set to true creates a new document when no document matches the filter (default: false)
run_command(pool, query, opts \\ [])

Issue a database command. If the command has parameters use a keyword list for the document because the “command key” has to be the first in the document.

save_many(pool, coll, docs, opts \\ [])

Updates documents or inserts them.

For the documents that does not contain the _id field, insert_many/3 function is used to persist them, for those that do contain the _id field, the replace_one/5 function is invoked for each document separately, where the filter is the _id field, and the :upsert option is set to true.

Options

  • :ordered - if set to false will group all documents to be inserted together, otherwise it will preserve the order, but it may be slow for large number of documents (default: false)
save_one(pool, coll, doc, opts \\ [])

Updates an existing document or inserts a new one.

If the document does not contain the _id field, then the insert_one/3 function is used to persist the document, otherwise replace_one/5 is used, where the filter is the _id field, and the :upsert option is set to true.

update_many(pool, coll, filter, update, opts \\ [])

Update all documents matching the filter.

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

Options

  • :upsert - if set to true creates a new document when no document matches the filter (default: false)
update_one(pool, coll, filter, update, opts \\ [])

Update a single document matching the filter.

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

Example:

Mongo.update_one(MongoPool,
  "my_test_collection",
  %{"filter_field": "filter_value"},
  %{"$set": %{"modified_field": "new_value"}})

Options

  • :upsert - if set to true creates a new document when no document matches the filter (default: false)