About this guide

This guide covers:

  • Deleting documents with Monger
  • Deleting a single document vs multiple documents
  • Working with multiple databases

This work is licensed under a Creative Commons Attribution 3.0 Unported License (including images & stylesheets). The source is available on Github.

What version of Monger does this guide cover?

This guide covers Monger 1.7 (including beta releases).

How to Remove Documents with Monger

Documents are removed using monger.collection/remove function that takes a collection name and conditions:

(ns my.service.server
  (:require [monger.collection :as mc]
            [monger.core :refer [connect! connect set-db! get-db]])
  (:import [org.bson.types ObjectId]))

;; localhost, default port
(connect!)
(set-db! (monger.core/get-db "monger-test"))

;; insert a few documents
(mc/insert "documents" { :language "English" :pages 38 })
(mc/insert "documents" { :language "Spanish" :pages 78 })
(mc/insert "documents" { :language "Unknown" :pages 87 })

;; remove multiple documents
(mc/remove "documents" { :language "English" })

;; remove ALL documents in the collection
(mc/remove "documents")

Removing a Single Document By Id

monger.collection/remove-by-id is useful when document id is known:

(ns my.service.server
  (:require [monger.core :refer [connect! connect set-db! get-db]]
            [monger.collection :refer [insert remove-by-id] :as mc])
  (:import [org.bson.types ObjectId]))

;; localhost, default port
(connect!)
(set-db! (monger.core/get-db "monger-test"))

;; remove document by id
(let [oid (ObjectId.)]
  (insert "documents" { :language "English" :pages 38 :_id oid })
  (remove-by-id "documents" oid))

Working With Multiple Databases

Monger is optimized for applications that use only one database but it is possible to work with multiple ones.

For that, use functions in the monger.multi.collection namespace: they mirror monger.collection but take a database as the first argument.

It is also possible to use clojure.core/binding to rebind monger.core/*mongodb-database*, monger.core/*mongodb-connection* and monger.core/*mongodb-gridfs* vars to different values or use convenience functions that do that: monger.core/with-connection, monger.core/with-db, monger.core/with-gridfs. This is a common practice for Clojure libraries. Remember that var bindings are thread-local.

What to read next

The documentation is organized as a number of guides, covering all kinds of topics.

We recommend that you read the following guides first, if possible, in this order:

Tell Us What You Think!

Please take a moment to tell us what you think about this guide on Twitter or the Monger mailing list

Let us know what was unclear or what has not been covered. Maybe you do not like the guide style or grammar or discover spelling mistakes. Reader feedback is key to making the documentation better.

comments powered by Disqus