In this post, we’ll have a first look at mongo db. So first, you’ll want to install it. As starting point you can go to the [MongoDB Homepage Community Edition download page][mongodb-download]. There download the newest installer for your operating system. If you use Linux, go to the Linux Download Page where you’ll get instructions on how to install MongoDB with your package manager. If you don’t want to install it on your own machine, MongoDB also offers to host your instance on its cloud [MongoDB Atlas][mongo-atlas] (free until 512MB storage, paid offerings if you need more).

After you have installed mongo on your PC, start the MongoDB service (e.g. mongod in your Linux console, may be already started automatically after installation).

Then open another command line and open the Mongo Shell by typing mongo in your command line.

There you can type “help” to get a list of commands.

We’ll start by choosing which database to use:

> use my_new_db

Next we’ll create a collection. This is just done by inserting something into it, so we’ll just do it and insert something in a new collection called people.

> db.people.insert( {name: "Philippe", profession: "Software Developer"} )

After pressing ENTER, you’ll see something like this:

WriteResult({ "nInserted" : 1 })

Congratulations, you’ve just inserted your first document into a collection!

You can now get the content of your collection, by using the function find():

> db.people.find()

which will return something like this (probably a different ObjectId):

{ "_id" : ObjectId("5b7f10b19abd168579ed0bde"), "name" : "Philippe", "profession" : "Software Developer" }

As you can see, Mongo has generated an _id for us. We can also add one ourselves:

> db.people.insert( {name: "Emanuel", profession: "President", _id: "1"} )
WriteResult({ "nInserted" : 1 })
> db.people.find()
{ "_id" : ObjectId("5b7f10b19abd168579ed0bde"), "name" : "Philippe", "profession" : "Software Developer" }
{ "_id" : "1", "name" : "Emanuel", "profession" : "President" }

Of course we can not only return the entire collection, but also do some queries. For this find takes two parameters: the first one specifies what you search, the second one is the projection, the attributes you want to retrieve.

> db.people.find({profession: "President"},{name: 1})
{ "_id" : "1", "name" : "Emanuel" }

In the projection, you decide which fields you want to show and which to hide. By specifying {name: 1} we showed, that we wanted to see the name. If you want to hide a field, you have to use 0 as value. The _id field is shown by default, so if we only want to see the name, we have to use following query:

> db.people.find({profession: "President"},{name: 1, _id: 0})
{ "name" : "Emanuel" }

If we now want to add the age to a document, we can update it like this:

> db.people.update({_id: "1"},{$set:{age: 40}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.people.find({_id: "1"})
{ "_id" : "1", "name" : "Emanuel", "profession" : "President", "age" : 40 }

You can unset an attribute with $unset:

> db.people.update({_id: "1"},{$unset: {profession: "value does not matter"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.people.find({_id: "1"})
{ "_id" : "1", "name" : "Emanuel", "age" : 40 }

Finally to remove a document, you use the function remove

> db.people.find().count()
2
> db.people.remove({name: "Philippe"})
WriteResult({ "nRemoved" : 1 })
> db.people.find().count()
1

Now you know how to peform basic CRUD operations. In the next post, we’ll look into aggregate queries and into more complex queries than just matching attributes.