Tag Archives: Mongo

Using the Mongo DB Shell to remove all elements in a collection

In my recent experiments with using MongoDB, I came across a need to remove all the records from a collection – the equivalent of DELETE FROM MYTABLE in SQL.

It turns out that, in order to do this, you need to use the Mongo DB Shell. This comes with Compass, although you may miss it, tucked away at the bottom:

By default it’s collapsed, but once you un-collapse it, you can do a number of things. The first thing you should do is tell it which DB you would like to use (for the purpose of this, let’s assume the database is called aardvark):

> use aardvark
< 'switched to db aardvark'

You can see which collections you have in the database, like this:

> db.getCollectionNames()
< [ 'collection1', 'collection2' ]

You can also see some information about a specific collection; for example:

> db.collection1.exists()

This should return a JSON document that details collection1.

In my case, I wanted to clear everything from the collection:

> db.collection1.deleteMany({ })

You can also pass this a specific filter, but this allows you to delete everything.




Installing and Running MongoDB from Scratch

I’m relatively new to Mongo, but I’ve decided to use it for a recent project, mainly because it writes quickly. I wanted to install and run it locally; however, most of the tutorials for Mongo push Atlas pretty heavily, so I thought it might be worth creating a post on installing and running Mongo from scratch. Most of what’s here is a duplication of what you can find in the official docs.


Start by downloading Mongo from here.

You’re then given some options during the installation. If you decide to change any of the default directories, then make a note of what you change them to; otherwise just accept the defaults:

This process will install MongoDb Compass by default – this is Mongo’s equivalent of SQL Server Management Studio, or MySQL’s SQL Workbench.

Running the Service

Mongo will install a Windows Service by default (well, if you look closely at the screenshot above, you’ll see that it’s pretty easy to stop it doing this), and you can just connect to this; however, you can remove that service (just go into Windows -> Services and remove it).

If you choose to keep the default installation then skip down to the Compass step.

In order to run Mongo yourself, you’ll need to run a command window, and navigate to the Mongo installation directory (typically C:\Program Files\MongoDB\Server\4.4\bin):

The key files here are:
– mongo.exe which is a mongo client
– mongod.exe which is the database service

The first thing to do is to start the DB Service:

.\mongod.exe –dbpath=”c:\tmp\mongodata”

This will use c:\tmp to store the data in.

Running the Client and Manipulating the Data

Now that the server is running, you can launch the client:

First, launch a command prompt as admin.

Next navigate to: C:\Program Files\MongoDB\Server\4.4\bin\

PS C:\Program Files\MongoDB\Server\4.4\bin> .\mongo.exe

This is a command line client. To create a new DB, just tell it to use a database that doesn’t exist:

> use testdb
switched to db testdb
> db.testcollection.insertOne({test: "test", test2: 1})
        "acknowledged" : true,
        "insertedId" : ObjectId("60830bfe34473a83b0fe56a6")

Here, we’ve inserted an object into the database; we can view that object by using something like the following:

> db.testcollection.find()
{ "_id" : ObjectId("60830bfe34473a83b0fe56a6"), "test" : "test", "test2" : 1 }

Finally, we can do the same thing, but in a GUI, called Compass.


After launching Compass for the first time, select New Connection, and then Fill in connection fields individually:

The following will work if you’ve just installed the default (as above) and made no config changes:

We can now browse the data that we inserted earlier: