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.6 (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])
  (:use [monger.core :only [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
  (:use [monger.core :only [connect! connect set-db! get-db]]
        [monger.collection :only [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))

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