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
aggregate(pool, coll, pipeline, opts \\ []) | Performs aggregation operation using the aggregation pipeline |
count(pool, coll, filter, opts \\ []) | Returns the count of documents that would match a |
delete_many(pool, coll, filter, opts \\ []) | Remove all documents matching the filter from the collection |
delete_one(pool, coll, filter, opts \\ []) | Remove a document matching the filter from the collection |
distinct(pool, coll, field, filter, opts \\ []) | Finds the distinct values for a specified field across a collection |
find(pool, coll, filter, opts \\ []) | Selects documents in a collection and returns a cursor for the selected documents |
insert_many(pool, coll, docs, opts \\ []) | Insert multiple documents into the collection |
insert_one(pool, coll, doc, opts \\ []) | Insert a single document into the collection |
replace_one(pool, coll, filter, replacement, opts \\ []) | Replace a single document matching the filter with the new document |
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 |
save_one(pool, coll, doc, opts \\ []) | Updates an existing document or inserts a new one |
update_many(pool, coll, filter, update, opts \\ []) | Update all documents matching the filter |
update_one(pool, coll, filter, update, opts \\ []) | Update a single document matching the filter |
Types
Functions
Specs:
- aggregate(Mongo.Pool.t, collection, [BSON.document], Keyword.t) :: cursor
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)
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
Specs:
- delete_many(Mongo.Pool.t, collection, BSON.document, Keyword.t) :: :ok | {:ok, Mongo.DeleteResult.t}
Remove all documents matching the filter from the collection.
Specs:
- delete_one(Mongo.Pool.t, collection, BSON.document, Keyword.t) :: :ok | {:ok, Mongo.DeleteResult.t}
Remove a document matching the filter from the collection.
Specs:
- distinct(Mongo.Pool.t, collection, String.t | atom, BSON.document, Keyword.t) :: [BSON.t]
Finds the distinct values for a specified field across a collection.
Options
:max_time
- Specifies a time limit in milliseconds
Specs:
- find(Mongo.Pool.t, collection, BSON.document, Keyword.t) :: cursor
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)
Specs:
- insert_many(Mongo.Pool.t, collection, [BSON.document], Keyword.t) :: :ok | {:ok, Mongo.InsertManyResult.t}
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
)
Specs:
- insert_one(Mongo.Pool.t, collection, BSON.document, Keyword.t) :: :ok | {:ok, Mongo.InsertOneResult.t}
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.
Specs:
- replace_one(Mongo.Pool.t, collection, BSON.document, BSON.document, Keyword.t) :: :ok | {:ok, Mongo.UpdateResult.t}
Replace a single document matching the filter with the new document.
Options
:upsert
- if set totrue
creates a new document when no document matches the filter (default:false
)
Specs:
- run_command(Mongo.Pool.t, BSON.document, Keyword.t) :: BSON.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.
Specs:
- save_many(Mongo.Pool.t, collection, BSON.document, Keyword.t) :: :ok | {:ok, Mongo.SaveManyResult.t}
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 tofalse
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
)
Specs:
- save_one(Mongo.Pool.t, collection, BSON.document, Keyword.t) :: :ok | {:ok, Mongo.SaveOneResult.t}
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
.
Specs:
- update_many(Mongo.Pool.t, collection, BSON.document, BSON.document, Keyword.t) :: :ok | {:ok, Mongo.UpdateResult.t}
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 totrue
creates a new document when no document matches the filter (default:false
)
Specs:
- update_one(Mongo.Pool.t, collection, BSON.document, BSON.document, Keyword.t) :: :ok | {:ok, Mongo.UpdateResult.t}
Update a single document matching the filter.
Uses MongoDB update operators to specify the updates. For more information please refer to the MongoDB documentation
Options
:upsert
- if set totrue
creates a new document when no document matches the filter (default:false
)