2019-11-14 02:01:38 +00:00
|
|
|
# mongo-driver-3
|
|
|
|
|
|
2019-11-14 03:40:39 +00:00
|
|
|
[](https://clojars.org/mongo-driver-3)
|
|
|
|
|
|
2019-11-17 06:17:43 +00:00
|
|
|
[](https://cljdoc.org/d/mongo-driver-3/mongo-driver-3/CURRENT)
|
2019-11-17 06:04:37 +00:00
|
|
|
|
|
|
|
|
|
2019-11-14 02:01:38 +00:00
|
|
|
A Mongo client for clojure, lightly wrapping 3.11+ versions of the [MongoDB Java Driver](https://mongodb.github.io/mongo-java-driver/)
|
|
|
|
|
|
|
|
|
|
In general, it will feel familiar to users of mongo clients like [monger](https://github.com/michaelklishin/monger).
|
|
|
|
|
Like our HTTP/2 client [hato](https://github.com/gnarroway/hato), the API is designed to be idiomatic and to make common
|
|
|
|
|
tasks convenient, whilst still allowing the underlying client to be configured via native Java objects.
|
|
|
|
|
|
|
|
|
|
It was developed with the following goals:
|
|
|
|
|
|
2019-11-17 06:00:30 +00:00
|
|
|
- Simple
|
2019-11-14 02:01:38 +00:00
|
|
|
- Up to date with the latest driver versions
|
2019-11-17 06:00:30 +00:00
|
|
|
- Minimal layer that does not prevent access to the underlying driver
|
2019-11-14 02:01:38 +00:00
|
|
|
- Consistent API across all functions
|
|
|
|
|
- Configuration over macros
|
2019-11-17 06:00:30 +00:00
|
|
|
|
2019-11-14 02:01:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
## Status
|
|
|
|
|
|
2019-11-14 12:09:01 +00:00
|
|
|
mongo-driver-3 is under active development and the API may change.
|
2019-11-14 02:01:38 +00:00
|
|
|
Please try it out and raise any issues you may find.
|
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
For Leinengen, add this to your project.clj:
|
|
|
|
|
|
|
|
|
|
```clojure
|
|
|
|
|
;; The underlying driver -- any newer version can also be used
|
2019-11-15 00:53:13 +00:00
|
|
|
[org.mongodb/mongodb-driver-sync "3.11.2"]
|
2019-11-14 02:01:38 +00:00
|
|
|
|
|
|
|
|
;; This wrapper library
|
2019-11-17 06:04:37 +00:00
|
|
|
[mongo-driver-3 "0.3.1"]
|
2019-11-14 02:01:38 +00:00
|
|
|
```
|
|
|
|
|
|
2019-11-17 06:00:30 +00:00
|
|
|
## Getting started
|
|
|
|
|
|
|
|
|
|
We usually start by creating a client and connecting to a database with a connection string.
|
|
|
|
|
`connect-to-db` is a convenience function that allows you to do this directly.
|
|
|
|
|
|
|
|
|
|
```clojure
|
|
|
|
|
(ns my.app
|
|
|
|
|
(:require [mongo-driver-3.client :as mcl]))
|
|
|
|
|
|
|
|
|
|
(mcl/connect-to-db "mongodb://localhost:27017/my-db")
|
|
|
|
|
; =>
|
|
|
|
|
; {
|
|
|
|
|
; :client - a MongoClient instance
|
|
|
|
|
; :db - a Database that you can pass to all the collection functions
|
|
|
|
|
; }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
You can also create a client and get a DB manually:
|
|
|
|
|
|
|
|
|
|
```clojure
|
|
|
|
|
;; Calling create without an arg will try and connect to the default host/port.
|
|
|
|
|
(def client (mcl/create "mongodb://localhost:27017"))
|
|
|
|
|
|
|
|
|
|
;; Create a db that you can pass around.
|
2019-11-17 06:06:57 +00:00
|
|
|
(def db (mcl/get-db client "my-db"))
|
2019-11-17 06:00:30 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Collection functions
|
|
|
|
|
|
2019-11-17 07:36:10 +00:00
|
|
|
All the collection functions closely mirror the naming in the corresponding java driver
|
2019-11-17 06:00:30 +00:00
|
|
|
[module](https://mongodb.github.io/mongo-java-driver/3.11/javadoc/com/mongodb/client/MongoCollection.html).
|
|
|
|
|
|
|
|
|
|
They always take a db as the first argument, collection name as the second,
|
|
|
|
|
and an optional map of options as the last. Full documentation of options can be found on
|
|
|
|
|
[cljdoc](https://cljdoc.org/d/mongo-driver-3/mongo-driver-3/CURRENT/api/mongo-driver-3.collection).
|
|
|
|
|
|
|
|
|
|
As an example:
|
|
|
|
|
|
|
|
|
|
```clojure
|
|
|
|
|
(ns my.app
|
|
|
|
|
(:require [mongo-driver-3.collection :as mc]))
|
|
|
|
|
|
|
|
|
|
;; Insert some documents
|
|
|
|
|
(mc/insert-many db "test" [{:v "hello"} {:v "world"}])
|
|
|
|
|
|
|
|
|
|
;; Count all documents
|
|
|
|
|
(mc/count-documents db "test")
|
|
|
|
|
; => 2
|
|
|
|
|
|
|
|
|
|
;; Count with a query
|
|
|
|
|
(mc/count-documents db "test" {:v "hello"})
|
|
|
|
|
; => 1
|
|
|
|
|
|
|
|
|
|
;; Find the documents, returning a seq
|
2019-11-17 07:36:10 +00:00
|
|
|
(mc/find db "test" {} {:limit 1 :projection {:_id 0}})
|
2019-11-17 06:00:30 +00:00
|
|
|
; => ({:v "hello"})
|
|
|
|
|
|
|
|
|
|
;; Find the documents, returning the raw FindIterable response
|
2019-11-17 07:36:10 +00:00
|
|
|
(mc/find db "test" {} {:raw? true})
|
2019-11-17 06:00:30 +00:00
|
|
|
; => a MongoIterable
|
|
|
|
|
|
|
|
|
|
;; Find a single document or return nil
|
2019-11-17 06:06:57 +00:00
|
|
|
(mc/find-one db "test" {:v "world"} {:keywordize? false})
|
2019-11-17 06:00:30 +00:00
|
|
|
; => {"v" "world"}
|
|
|
|
|
```
|
|
|
|
|
|
2019-11-17 06:21:52 +00:00
|
|
|
While most options are supported directly, sometimes you may need to some extra control.
|
|
|
|
|
In such cases, you can pass in a configured java options object as option. Any other
|
|
|
|
|
options will be applied on top of this object.
|
2019-11-17 06:00:30 +00:00
|
|
|
|
|
|
|
|
```clojure
|
|
|
|
|
;; These are equivalent
|
|
|
|
|
(mc/rename db "test" "new-test" {:drop-target? true})
|
|
|
|
|
|
|
|
|
|
(mc/rename db "test" "new-test" {:rename-collection-options (.dropTarget (RenameCollectionOptions.) true)})
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Again, read the [docs](https://cljdoc.org/d/mongo-driver-3/mongo-driver-3/CURRENT/api/mongo-driver-3.collection)
|
|
|
|
|
for full API documentation.
|
|
|
|
|
|
2019-11-14 02:01:38 +00:00
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
|