monger.collection

Provides key functionality for interaction with MongoDB: inserting, querying, updating and deleting documents, performing Aggregation Framework
queries, creating and dropping indexes, creating collections and more.

For more advanced read queries, see monger.query.

Related documentation guides:

* http://clojuremongodb.info/articles/getting_started.html
* http://clojuremongodb.info/articles/inserting.html
* http://clojuremongodb.info/articles/querying.html
* http://clojuremongodb.info/articles/updating.html
* http://clojuremongodb.info/articles/deleting.html
* http://clojuremongodb.info/articles/aggregation.html

aggregate

(aggregate db coll stages & opts)
Executes an aggregation query. MongoDB 2.2+ only.
 Accepts the options :allow-disk-use and :cursor (a map with the :batch-size
 key), as described in the MongoDB manual. Additionally, the :max-time option
is supported, for specifying a limit on the execution time of the query in
milliseconds.

See http://docs.mongodb.org/manual/applications/aggregation/ to learn more.

any?

(any? db coll)(any? db coll conditions)
Whether the collection has any items at all, or items matching query.

count

(count db coll)(count db coll conditions)
Returns the number of documents in this collection.

Takes optional conditions as an argument.

create

(create db coll options)
Creates a collection with a given name and options.

Options are:

:capped (pass true to create a capped collection)
:max (number of documents)
:size (max allowed size of the collection, in bytes)

create-index

(create-index db coll keys)(create-index db coll keys options)
Forces creation of index on a set of fields, if one does not already exists.

distinct

(distinct db coll key)(distinct db coll key query)
Finds distinct values for a key

drop

(drop db coll)
Deletes collection from database.

drop-index

(drop-index db coll idx)
Drops an index from this collection.

drop-indexes

(drop-indexes db coll)
Drops all indixes from this collection.

empty?

(empty? db coll)
Whether the collection is empty.

ensure-index

(ensure-index db coll keys)(ensure-index db coll keys options)(ensure-index db coll keys index-name unique?)
Creates an index on a set of fields, if one does not already exist.
This operation is inexpensive in the case when an index already exists.

Options are:

:unique (boolean) to create a unique index
:name (string) to specify a custom index name and not rely on the generated one

exists?

(exists? db coll)
Checks weather collection with certain name exists.

explain-aggregate

(explain-aggregate db coll stages & opts)
Returns the explain plan for an aggregation query. MongoDB 2.2+ only.

See http://docs.mongodb.org/manual/applications/aggregation/ to learn more.

find

(find db coll)(find db coll ref)(find db coll ref fields)
Queries for objects in this collection.
This function returns DBCursor, which allows you to iterate over DBObjects.
If you want to manipulate clojure sequences maps, use find-maps.

find-and-modify

(find-and-modify db coll conditions document {:keys [fields sort remove return-new upsert keywordize], :or {fields nil, sort nil, remove false, return-new false, upsert false, keywordize true}})
Atomically modify a document (at most one) and return it.

find-by-id

(find-by-id db coll id)(find-by-id db coll id fields)
Returns a single object with matching _id field.

find-map-by-id

(find-map-by-id db coll id)(find-map-by-id db coll id fields)(find-map-by-id db coll id fields keywordize)
Returns a single object, converted to map with matching _id field.

find-maps

(find-maps db coll)(find-maps db coll ref)(find-maps db coll ref fields)(find-maps db coll ref fields keywordize)
Queries for objects in this collection.
This function returns clojure Seq of Maps.
If you want to work directly with DBObject, use find.

find-one

(find-one db coll ref)(find-one db coll ref fields)
Returns a single DBObject from this collection matching the query.

find-one-as-map

(find-one-as-map db coll ref)(find-one-as-map db coll ref fields)(find-one-as-map db coll ref fields keywordize)
Returns a single object converted to Map from this collection matching the query.

find-seq

(find-seq db coll)(find-seq db coll ref)(find-seq db coll ref fields)
Queries for objects in this collection, returns ISeq of DBObjects.

indexes-on

(indexes-on db coll)
Return a list of the indexes for this collection.

insert

(insert db coll document)(insert db coll document concern)
Saves document to collection and returns a write result monger.result/acknowledged?
and related functions operate on. You can optionally specify a WriteConcern.

In case you need the exact inserted document returned, with the :_id key generated,
use monger.collection/insert-and-return instead.

insert-and-return

(insert-and-return db coll document)(insert-and-return db coll document concern)
Like monger.collection/insert but returns the inserted document as a persistent Clojure map.

If the :_id key wasn't set on the document, it will be generated and merged into the returned
map.

insert-batch

(insert-batch db coll documents)(insert-batch db coll documents concern)
Saves documents do collection. You can optionally specify WriteConcern as a third argument.

map-reduce

(map-reduce db coll js-mapper js-reducer output query)(map-reduce db coll js-mapper js-reducer output output-type query)
Performs a map reduce operation

purge-many

(purge-many db xs)
Purges (removes all documents from) multiple collections. Intended
to be used in test environments.

remove

(remove db coll)(remove db coll conditions)
Removes objects from the database.

remove-by-id

(remove-by-id db coll id)
Removes a single document with given id

rename

(rename db from to)(rename db from to drop-target?)
Renames collection.

save

(save db coll document)(save db coll document write-concern)
Saves an object to the given collection (does insert or update based on the object _id).

If the object is not present in the database, insert operation will be performed.
If the object is already in the database, it will be updated.

This function returns write result. If you want to get the exact persisted document back,
use `save-and-return`.

save-and-return

(save-and-return db coll document)(save-and-return db coll document write-concern)
Saves an object to the given collection (does insert or update based on the object _id).

If the object is not present in the database, insert operation will be performed.
If the object is already in the database, it will be updated.

This function returns the exact persisted document back, including the `:_id` key in
case of an insert.

If you want to get write result back, use `save`.

system-collection-pattern

system-collection?

(system-collection? coll)
Evaluates to true if the given collection name refers to a system collection. System collections
are prefixed with system. or fs. (default GridFS collection prefix)

update

(update db coll conditions document)(update db coll conditions document {:keys [upsert multi write-concern], :or {upsert false, multi false, write-concern mc/*mongodb-write-concern*}})
Performs an update operation.

Please note that update is potentially destructive operation. It updates document with the given set
emptying the fields not mentioned in the new document. In order to only change certain fields, use
"$set".

You can use all the MongoDB modifier operations ($inc, $set, $unset, $push, $pushAll, $addToSet, $pop, $pull
$pullAll, $rename, $bit) here as well.

It also takes options, such as :upsert and :multi.
By default :upsert and :multi are false.

update-by-id

(update-by-id db coll id document)(update-by-id db coll id document {:keys [upsert write-concern], :or {upsert false, write-concern mc/*mongodb-write-concern*}})
Update a document with given id

update-by-ids

(update-by-ids db coll ids document)(update-by-ids db coll ids document {:keys [upsert write-concern], :or {upsert false, write-concern mc/*mongodb-write-concern*}})
Update documents by given ids

upsert

(upsert db coll conditions document)(upsert db coll conditions document {:keys [multi write-concern], :or {multi false, write-concern mc/*mongodb-write-concern*}})
Performs an upsert.

This is a convenience function that delegates to monger.collection/update and
sets :upsert to true.

See monger.collection/update documentation