Start the mongo shell
Start the mongo shell by running mongo
Note: this will connect using the default port 27017. Use -p for specifying a different port.
You should get an output similar to the following
MongoDB shell version v4.0.3 connecting to: mongodb://127.0.0.1:27017 Implicit session: session { "id" : UUID("2323-8as8-23aa-8fsa") } MongoDB server version: 4.0.3 >
From this moment on, everything you’ll type will be executed in the mongo shell.
List databases
Your server may host several different databases you have access to. Run show dbs
to list the databases
> show dbs admin 0.000GB config 0.000GB fullstack 0.000GB local 0.000GB rhoci 0.000GB
There are two databases which I created: rhoci and fullstack. The rest is created by default and you should see them in your environment as well.
Switch database
By default, you’ll be using the ‘test’ database. You can verify it by using the db
command. The reason it’s not listed in show dbs
is that it’s an empty database. Meaning, there are no documents in it.
In order to switch to another database, run use <DB_name>
> use rhoci switched to db rhoci
We switched from the data test
to the database rhoci
List collections
To list all the collections in your database simply run show collectinos
> show collections jobs
You can see we have one collection named ‘jobs’.
Query all documents
To query for all of our documents in the ‘jobs’ collection, we’ll use thefind
command with a blank expression.
> db.jobs.find({}) { "_id" : ObjectId("5c7e7de5a69b772f445113ff"), "name" : "job1", "created_date" : ISODate("2019-03-05T13:47:17.103Z") } { "_id" : ObjectId("5c7e7de5a69b772f44511400"), "name" : "job2", "created_date" : ISODate("2019-03-05T13:47:17.105Z") } { "_id" : ObjectId("5c7e7de5a69b772f44511401"), "name" : "job3", "created_date" : ISODate("2019-03-05T13:47:17.106Z") }
We have three 3 documents in ‘jobs’ collection. Each has an id, name, and created_date.
Query for a specific document
To query for one, specific document all you have to do is to specify equality conditions this way
> db.jobs.find({name: "job3"}) { "_id" : ObjectId("5c7e7de5a69b772f44511401"), "name" : "job3", "created_date" : ISODate("2019-03-05T13:47:17.106Z") }
We queried for a job whose name is “job3” and we got exactly one result as we have one document with the name ‘job3’.
Query using a regular expression
Sometimes you will want to query for certain pattern rather than a specific document. Mongo supports regular expressions.
For example, the following command result will match only job1 and job2
> db.jobs.find({name: /job[1-2]/}) { "_id" : ObjectId("5c7e7de5a69b772f445113ff"), "name" : "job1", "created_date" : ISODate("2019-03-05T13:47:17.103Z") } { "_id" : ObjectId("5c7e7de5a69b772f44511400"), "name" : "job2", "created_date" : ISODate("2019-03-05T13:47:17.105Z") }
Now let’s query for jobs whose name contains the digit 3
> db.jobs.find({name: /3/}) { "_id" : ObjectId("5c7e7de5a69b772f44511401"), "name" : "job3", "created_date" : ISODate("2019-03-05T13:47:17.106Z") }
Insert a new document into a collection
To insert a single document into your collection, use db.<collection_name>.insertOne{...}
> db.jobs.insertOne({ name: 'job100'}) { "acknowledged" : true, "insertedId" : ObjectId("5c7fc155ef1b6e34f192") } > db.jobs.find({name: 'job100'}) { "_id" : ObjectId("5c7fc155ef1b6e34f192"), "name" : "job100" }
Insert several documents at once
Similarly to what we did in the previous section, you can use db.<collection_name>.insertMany
for inserting several documents
> db.jobs.insertMany([ ... { name: 'job200' }, ... { name: 'job300' }, ... ]) { "acknowledged" : true, "insertedIds" : [ ObjectId("5c7fc26aef531b6e34f193"), ObjectId("5c7fc26aef531b6e34f194") ]} > db.jobs.find({}) { "_id" : ObjectId("5c7fc155ef531b6e34f192"), "name" : "job100" } { "_id" : ObjectId("5c7fc26aef531b6e34f193"), "name" : "job200" } { "_id" : ObjectId("5c7fc26aef531b6e34f194"), "name" : "job300" }
Delete all documents
The following will remove all documents from ‘jobs’ collection
> db.jobs.deleteMany({}) { "acknowledged" : true, "deletedCount" : 3 }
You can see it removed a total of 3 documents.
Delete one specific document
The following will remove only ‘job100’ document
> db.jobs.deleteOne( { name: "job100" } ) { "acknowledged" : true, "deletedCount" : 1 }
Try for yourself: what happens when you have two documents with job name ‘job100’ and you run the above command?
Delete documents using regex
Let’s remove all the documents of jobs whose name includes 2 and above
> db.jobs.deleteOne( { name: /[3-9]/ } ) { "acknowledged" : true, "deletedCount" : 1 }
Delete collection
To remove a collection, use the db.<collection_name>.drop()
syntax.
> db.jobs.drop() true
If the collection was deleted successfully, you’ll get ‘true’. Otherwise you would get ‘false’ and that could mean a couple of things. The most common one is probably a simple typo in collection name.
That’s it. If you enjoyed reading this post or have any additional questions, feel free to leave a comment below.