(aggregate collection pipeline)MongoDB aggregation.
| Parameter | Description |
|---|---|
collection | keyword/string Collection name. |
pipeline | list {keyword/string {}} A list containing the request pipeline documents. |
Returns
Aggregation result.
Examples
(aggregate :users
[{:$match {:age {:$gte 20}}}
{:$project {:_id 0
:name 1}}])
MongoDB aggregation.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` Collection name. |
| `pipeline` | `list {keyword/string {}}` A list containing the request pipeline documents. |
**Returns**
Aggregation result.
**Examples**
```Clojure
(aggregate :users
[{:$match {:age {:$gte 20}}}
{:$project {:_id 0
:name 1}}])
```(create-index! collection
keys
&
:background b
:name s
:partial-filter-expression {}
:sparse b
:unique b)Create an index for a collection.
| Parameter | Description |
|---|---|
collection | keyword/string Collection name. |
keys | map/list(keyword/string) A document or a list of keywords or strings. |
:background | optional boolean Create the index in the background. Default false. |
:name | optional string A custom name for the index. Default false. |
:partial-filter-expression | optional map A filter expression for the index. |
:sparse | optional boolean Allow null values. Default false. |
:unique | optional boolean Index values must be unique. Default false. |
Returns
The index name.
Examples
(create-index!)
Create an index for a collection. | Parameter | Description | | --- | --- | | `collection` | `keyword/string` Collection name. | | `keys` | `map/list(keyword/string)` A document or a list of keywords or strings. | | `:background` | `optional boolean` Create the index in the background. Default `false`. | | `:name` | `optional string` A custom name for the index. Default `false`. | | `:partial-filter-expression` | `optional map` A filter expression for the index. | | `:sparse` | `optional boolean` Allow null values. Default `false`. | | `:unique` | `optional boolean` Index values must be unique. Default `false`. | **Returns** The index name. **Examples** ```Clojure (create-index!) ```
(delete! collection query)Delete matching documents.
| Parameter | Description |
|---|---|
collection | keyword/string The collection. |
query | map A standard MongoDB query. |
Returns
{:deleted-count <number of matching documents>}
Delete matching documents.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
**Returns**
```Clojure
{:deleted-count <number of matching documents>}
```(delete-one! collection query)Delete first matching document.
| Parameter | Description |
|---|---|
collection | keyword/string The collection. |
query | map A standard MongoDB query. |
Returns
{:deleted-count <number of matching documents>}
Delete first matching document.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
**Returns**
```Clojure
{:deleted-count <number of matching documents>}
```(fetch collection)(fetch collection query & :limit n :only {} :skip n :sort {})Fetch documents from collection.
| Parameter | Description |
|---|---|
collection | keyword/string The collection. |
query | {keyword/string object} A standard MongoDB query. |
:limit | optional int Number of documents to fetch. |
:only | optional {keyword/string object} A MongoDB map of fields to include or exclude. |
:skip | optional int Number of documents to skip before fetching. |
:sort | optional {keyword/string object} A MongoDB map of sorting criteria. |
Returns
A lazy sequence of matching documents.
Examples
; Fetch five documents from collection :users
(fetch :users {} :limit 5)
Fetch documents from collection.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `{keyword/string object}` A standard MongoDB query. |
| `:limit` | `optional int` Number of documents to fetch. |
| `:only` | `optional {keyword/string object}` A MongoDB map of fields to include or exclude. |
| `:skip` | `optional int` Number of documents to skip before fetching. |
| `:sort` | `optional {keyword/string object}` A MongoDB map of sorting criteria. |
**Returns**
A lazy sequence of matching documents.
**Examples**
```Clojure
; Fetch five documents from collection :users
(fetch :users {} :limit 5)
```(fetch-and-update! collection query & :return-new? b :upsert? b)Update first matching document.
| Parameter | Description |
|---|---|
collection | keyword/string The collection. |
query | map A standard MongoDB query. |
return-new? | optional boolean Return the updated document? Default if false. |
:upsert? | optional boolean If no document is found, create a new one. Default is false. |
Returns
A single document or nil.
Update first matching document. | Parameter | Description | | --- | --- | | `collection` | `keyword/string` The collection. | | `query` | `map` A standard MongoDB query. | | `return-new?` | `optional boolean` Return the updated document? Default if `false`. | | `:upsert?` | `optional boolean` If no document is found, create a new one. Default is `false`. | **Returns** A single document or nil.
(fetch-count collection)(fetch-count collection query)Count the number of documents returned.
| Parameter | Description |
|---|---|
collection | keyword/string The collection. |
query | {keyword/string object} A standard MongoDB query. |
Returns
Number of matching documents.
Count the number of documents returned.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `{keyword/string object}` A standard MongoDB query. |
**Returns**
Number of matching documents.(fetch-one collection)(fetch-one collection query)Return only the first document retrieved.
| Parameter | Description |
|---|---|
collection | keyword/string The collection. |
query | {keyword/string object} A standard MongoDB query. |
Returns
A single document or nil.
Return only the first document retrieved.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `{keyword/string object}` A standard MongoDB query. |
**Returns**
A single document or `nil`.(insert! collection document)(insert! collection document-list)Insert one document or a list thereof in a collection. Inserting a list is atomic.
| Parameter | Description |
|---|---|
collection | keyword/string The collection. |
document/s | map/list(map) A document or a list of documents. |
Returns
The document/s with _id fields, either a single document or a lazy sequence.
Examples
(insert! :users {:name "Alice"})
(insert! :users [{:name "Alice"}
{:name "Bob"}])
Insert one document or a list thereof in a collection. Inserting a list is atomic.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `document/s` | `map/list(map)` A document or a list of documents. |
**Returns**
The document/s with `_id` fields, either a single document or a lazy sequence.
**Examples**
```Clojure
(insert! :users {:name "Alice"})
(insert! :users [{:name "Alice"}
{:name "Bob"}])
```(replace-one! collection query document & :upsert? b)Replace a single document.
| Parameter | Description |
|---|---|
collection | keyword/string The collection. |
query | map A standard MongoDB query. |
document | map The new document. |
:upsert? | optional boolean If no document is found, create a new one. Default is false. |
Returns
{:matched-count <0 or 1>
:modified-count <0 or 1>}
Replace a single document.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `map` A standard MongoDB query. |
| `document` | `map` The new document. |
| `:upsert?` | `optional boolean` If no document is found, create a new one. Default is `false`. |
**Returns**
```Clojure
{:matched-count <0 or 1>
:modified-count <0 or 1>}
```(set-connection! uri)Procedurally set up or change mongodb connection.
uri - string: database location.
Procedurally set up or change mongodb connection. uri - string: database location.
(set-database! db)Procedurally set up or change database.
db - string: name of database to use.
Procedurally set up or change database. db - string: name of database to use.
(transaction & body)Functionally perform a transaction. Encapsulated database requests are queued and then atomically executed when the function goes out of scope.
Returns
The result of the last encapsulated expression.
Examples
(transaction
(insert! :users {:name "My Name"})
(fetch! :users))
Functionally perform a transaction. Encapsulated database requests are queued and then
atomically executed when the function goes out of scope.
**Returns**
The result of the last encapsulated expression.
**Examples**
```Clojure
(transaction
(insert! :users {:name "My Name"})
(fetch! :users))
```(update! collection query update & :upsert? b)Update matching documents.
| Parameter | Description |
|---|---|
collection | keyword/string The collection. |
query | {keyword/string object} A standard MongoDB query. |
update | {keyword/string object} A valid update document. Must use $set or $push, throws exception otherwise. |
:upsert? | optional boolean If no document is found, create a new one. Default is false. |
Returns
{:matched-count <number of matching documents>
:modified-count <number of modified documents>}
Examples
(update!)
Update matching documents.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `{keyword/string object}` A standard MongoDB query. |
| `update` | `{keyword/string object}` A valid update document. Must use `$set` or `$push`, throws exception otherwise. |
| `:upsert?` | `optional boolean` If no document is found, create a new one. Default is `false`. |
**Returns**
```Clojure
{:matched-count <number of matching documents>
:modified-count <number of modified documents>}
```
**Examples**
```Clojure
(update!)
```(update-one! collection query update & :upsert? b)Update first matching document.
| Parameter | Description |
|---|---|
collection | keyword/string The collection. |
query | {keyword/string object} A standard MongoDB query. |
update | {keyword/string object} A valid update document. Must use $set or $push. |
:upsert? | optional boolean If no document is found, create a new one. Default is false. |
Returns
{:matched-count <0 or 1>
:modified-count <0 or 1>}
Examples
(update-one!)
Update first matching document.
| Parameter | Description |
| --- | --- |
| `collection` | `keyword/string` The collection. |
| `query` | `{keyword/string object}` A standard MongoDB query. |
| `update` | `{keyword/string object}` A valid update document. Must use $set or $push. |
| `:upsert?` | `optional boolean` If no document is found, create a new one. Default is `false`. |
**Returns**
```Clojure
{:matched-count <0 or 1>
:modified-count <0 or 1>}
```
**Examples**
```Clojure
(update-one!)
```(with-db db & body)Functionally set up or change database. Reverts to earlier settings when leaving scope.
| Parameter | Description |
|---|---|
db | string Name of database to use. |
body | Encapsulated program calling the database. |
Returns
The result of the last encapsulated expression.
Examples
(with-db "my-database-2"
(insert! :users {:name "My Name"})
(fetch! :users))
Functionally set up or change database. Reverts to earlier settings when leaving scope.
| Parameter | Description |
| --- | --- |
| `db` | `string` Name of database to use. |
| `body` | Encapsulated program calling the database. |
**Returns**
The result of the last encapsulated expression.
**Examples**
```Clojure
(with-db "my-database-2"
(insert! :users {:name "My Name"})
(fetch! :users))
```(with-mongo uri db & body)Functionally set up or change mongodb connection. Reverts to earlier settings when leaving scope.
| Parameter | Description |
|---|---|
uri | string Database location. |
db | string Database to use. |
body | Encapsulated program utilizing the connection. |
Returns
The result of the last encapsulated expression.
Examples
(with-mongo "mongodb://localhost:27017" "my-database"
(insert! :users {:name "My Name"})
(fetch! :users))
Functionally set up or change mongodb connection. Reverts to earlier settings when leaving scope.
| Parameter | Description |
| --- | --- |
| `uri` | `string` Database location. |
| `db` | `string` Database to use. |
| `body` | Encapsulated program utilizing the connection. |
**Returns**
The result of the last encapsulated expression.
**Examples**
```Clojure
(with-mongo "mongodb://localhost:27017" "my-database"
(insert! :users {:name "My Name"})
(fetch! :users))
```cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |