Git Product home page Git Product logo

mongrove's Introduction

mongrove

drawing

A Clojure library designed to interact with MongoDB.

Status

Helpshift

Usage

(def client (connect :replica-set [{:host "localhost"
                                    :port 27017
                                    :opts {:read-preference :primary}}]))
(def test-db (get-db client "test_driver"))

(def mongo-coll "mongo")

(query test-db mongo-coll {} :sort-by {:age 1})

(count-docs test-db mongo-coll {:age {:$lt 10}})

(count-docs test-db mongo-coll {})

(doseq [i (range 10)]
  (insert test-db mongo-coll {:id i
                              :name (str "user-" i)
                              :age (rand-int 20)
                              :dob (java.util.Date.)} :multi? false))

(fetch-one test-db mongo-coll {:id 3} :only [:name])

(delete test-db mongo-coll {:age {:$gt 10}})

(update test-db mongo-coll {:age {:$lt 10}} {:$inc {:age 1}})

;; For using multi-document transactions,

(try
    (delete test-db "a" {})
    (delete test-db "b" {})
    ;; Creating new collections is not supported
    ;; in Mongo 4.0 so ensure that there exist collections
    ;; a and b
    (insert test-db "a" {:id 1})
    (insert test-db "b" {:id 1})

    (run-in-transaction client
                        (fn [session]
                          ;; DO NOT ADD try-catch here. If you do this, exceptions
                          ;; will not percolate to the transaction and it will get committed
                          ;; successfully
                          (insert test-db "a" {:a 42} :session session)
                          ;; This will throw an exception
                          (insert test-db "b" {:b (.toString nil)} :session session))
                        {:transaction-opts {:retry-on-errors true}})
    (catch Exception e
      (println "Data in collection a " (query test-db "a" {}))))

API

To view full API you can generate documentation using codox

Run

lein codox

from the project folder and the API documentation will be generated in the target/doc folder.

Tests

The test/mongrove folder contains unit tests for mongrove APIs. For running these tests, please make sure you have a running mongo cluster on localhost:27017 If you want to test against a different mongo cluster, please change the options in the test namespaces' init-connection-fixture function.

The test/mongrove/generative folder contains generative tests for Mongo query operators. These tests are primarily aimed comparing query results across 2 different Mongo clusters, something you might need to do when testing for an upgrade !

Next steps

  1. Add support for ACID transactions
  2. Add benchmarking results comparing against Monger
  3. Add support for sharded Mongo clusters

License

Copyright © Helpshift Inc. 2020

EPL (See LICENSE)

mongrove's People

Contributors

rhishikesh-helpshift avatar rhishikeshj avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.