In this post we will go through some basic examples on how to write, read, update and delete data with MongoDB using the Mongo Shell.

Pre-Requisites:

You will need to have a running MongoDB Server, you can follow this post to Setup MongoDB on Ubuntu

Access MongoDB using Mongo Shell:

$ mongo
>

Lets use a specific DB called sysadmins:

> use sysadmins
switched to db sysadmins

To determine which db are we using:

> db
sysadmins   

Inserting Records:

This is a simple record consisting of name and surname

> db.test.insert({name: "John", surname: "Dory"})
WriteResult({ "nInserted" : 1 })

In this example we will be adding users to our userdata collections:

> db.userdata.insert({
    name: 'Ruan',
    surname: 'Bekker',
    username: 'ruanb',
    nationality: 'South Africa',
    occupation: 'Sysadmin',
    gender: 'male',
    age: '1987/01/01',
    os: 'linux'
})

> db.userdata.insert({
    name: 'James',
    surname: 'Anderson',
    username: 'jamesa',
    nationality: 'New Zealand',
    occupation: 'Sysadmin',
    gender: 'male',
    age: '1987/04/02',
    os: 'linux'
})

> db.userdata.insert({
    name: 'Marge',
    surname: 'Adam',
    username: 'margea',
    nationality: 'South Africa',
    occupation: 'Sysadmin',
    gender: 'female',
    age: '1989/04/05',
    os: 'mac'
})
  
> db.userdata.insert({
    name: 'Kevin',
    surname: 'Dean',
    username: 'kevind',
    nationality: 'England',
    occupation: 'Developer',
    gender: 'male',
    age: '1992/04/05',
    os: 'linux'
})
  
> db.userdata.insert({
    name: 'David',
    surname: 'Koggelberg',
    username: 'davidk',
    nationality: 'Australia',
    occupation: 'Management',
    gender: 'male',
    age: '1985/04/05',
    os: 'linux'
})

> db.userdata.insert({
    name: 'Marge',
    surname: 'Adam',
    username: 'margea',
    nationality: 'South Africa',
    occupation: 'Sysadmin',
    gender: 'female',
    age: '1989/04/05',
    os: 'mac'
})

> db.userdata.insert({
    name: 'Samantha',
    surname: 'Frost',
    username: 'samt',
    nationality: 'New Zealand',
    occupation: 'Developer',
    gender: 'female',
    age: '1992/04/09',
    os: 'windows'
})

> db.userdata.insert({
    name: 'Mark', 
    surname: 'Beck', 
    username: 'markb', 
    nationality: 'South Africa', 
    occupation: 'Sysadmin',  
    gender: 'male', 
    age: '1989/04/05',  
    os: 'mac' 
})

Searching Records

Now that we have some sample data, we will do a scan to search for all records:

> db.userdata.find()
{ "_id" : ObjectId("56d0ae9cc806f4f7cfae1601"), "name" : "Ruan", "surname" : "Bekker", "username" : "ruanb", "nationality" : "South Africa", "occupation" : "Sysadmin", "gender" : "male", "age" : "1987/01/01", "os" : "linux" }
{ "_id" : ObjectId("56d0aecac806f4f7cfae1602"), "name" : "James", "surname" : "Anderson", "username" : "jamesa", "nationality" : "New Zealand", "occupation" : "Sysadmin", "gender" : "male", "age" : "1987/04/02", "os" : "linux" }
{ "_id" : ObjectId("56d0aecbc806f4f7cfae1603"), "name" : "Marge", "surname" : "Adam", "username" : "margea", "nationality" : "South Africa", "occupation" : "Sysadmin", "gender" : "female", "age" : "1989/04/05", "os" : "mac" }
{ "_id" : ObjectId("56d0aecbc806f4f7cfae1604"), "name" : "Kevin", "surname" : "Dean", "username" : "kevind", "nationality" : "England", "occupation" : "Developer", "gender" : "male", "age" : "1992/04/05", "os" : "linux" }
{ "_id" : ObjectId("56d0aecbc806f4f7cfae1605"), "name" : "David", "surname" : "Koggelberg", "username" : "davidk", "nationality" : "Australia", "occupation" : "Management", "gender" : "male", "age" : "1985/04/05", "os" : "linux" }
{ "_id" : ObjectId("56d0aecbc806f4f7cfae1606"), "name" : "Marge", "surname" : "Adam", "username" : "margea", "nationality" : "South Africa", "occupation" : "Sysadmin", "gender" : "female", "age" : "1989/04/05", "os" : "mac" }
{ "_id" : ObjectId("56d0aeccc806f4f7cfae1607"), "name" : "Samantha", "surname" : "Frost", "username" : "samt", "nationality" : "New Zealand", "occupation" : "Developer", "gender" : "female", "age" : "1992/04/09", "os" : "windows" }
{ "_id" : ObjectId("56d0b6f1c806f4f7cfae1608"), "name" : "Mark", "surname" : "Beck", "username" : "markb", "nationality" : "South Africa", "occupation" : "Sysadmin", "gender" : "male", "age" : "1989/04/05", "os" : "mac" }

Using Pretty Print:

> db.userdata.find().pretty()
...
{
        "_id" : ObjectId("56d0aeccc806f4f7cfae1607"),
        "name" : "Samantha",
        "surname" : "Frost",
        "username" : "samt",
        "nationality" : "New Zealand",
        "occupation" : "Developer",
        "gender" : "female",
        "age" : "1992/04/09",
        "os" : "windows"
}
...

Selectors:

Selectors are to Mongo what where clauses are to SQL

  • specify criteria that MUST match. i.e., an AND clause
  • specify criteria that CAN optionally match. i.e., an OR clause
  • specify criteria that MUST exist, etc.

We will use a basic selector that matches name = Ruan:

> db.userdata.find({name: "Ruan"}).pretty()
{
        "_id" : ObjectId("56d0ae9cc806f4f7cfae1601"),
        "name" : "Ruan",
        "surname" : "Bekker",
        "username" : "ruanb",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "male",
        "age" : "1987/01/01",
        "os" : "linux"
}

Find all users which uses Mac as OS:

> db.userdata.find({os: 'mac'}).pretty()
{
        "_id" : ObjectId("56d0aecbc806f4f7cfae1603"),
        "name" : "Marge",
        "surname" : "Adam",
        "username" : "margea",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "female",
        "age" : "1989/04/05",
        "os" : "mac"
}
{
        "_id" : ObjectId("56d0aecbc806f4f7cfae1606"),
        "name" : "Marge",
        "surname" : "Adam",
        "username" : "margea",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "female",
        "age" : "1989/04/05",
        "os" : "mac"
}

Multiple Criteria:

All users that use linux that's from Australia:

> db.userdata.find({os: 'linux', $or: [{nationality: 'Australia'}]}).pretty()
{
        "_id" : ObjectId("56d0aecbc806f4f7cfae1605"),
        "name" : "David",
        "surname" : "Koggelberg",
        "username" : "davidk",
        "nationality" : "Australia",
        "occupation" : "Management",
        "gender" : "male",
        "age" : "1985/04/05",
        "os" : "linux"
}

And all users that uses linux thats from Australia and England:

> db.userdata.find({os: 'linux', $or: [{nationality: 'Australia'},{nationality: 'England'}]}).pretty()
{
        "_id" : ObjectId("56d0aecbc806f4f7cfae1604"),
        "name" : "Kevin",
        "surname" : "Dean",
        "username" : "kevind",
        "nationality" : "England",
        "occupation" : "Developer",
        "gender" : "male",
        "age" : "1992/04/05",
        "os" : "linux"
}
{
        "_id" : ObjectId("56d0aecbc806f4f7cfae1605"),
        "name" : "David",
        "surname" : "Koggelberg",
        "username" : "davidk",
        "nationality" : "Australia",
        "occupation" : "Management",
        "gender" : "male",
        "age" : "1985/04/05",
        "os" : "linux"
}

Sorting:

Here we will list all male users thats from South Africa and England, but the names are sorted by descending order

> db.userdata.find({gender: 'male', $or: [{nationality: 'South Africa'},{nationality: 'England'}]}).sort({name: -1}).pretty()
{
        "_id" : ObjectId("56d0ae9cc806f4f7cfae1601"),
        "name" : "Ruan",
        "surname" : "Bekker",
        "username" : "ruanb",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "male",
        "age" : "1987/01/01",
        "os" : "linux"
}
{
        "_id" : ObjectId("56d0aecbc806f4f7cfae1604"),
        "name" : "Kevin",
        "surname" : "Dean",
        "username" : "kevind",
        "nationality" : "England",
        "occupation" : "Developer",
        "gender" : "male",
        "age" : "1992/04/05",
        "so" : "linux"
}

On Ascending order:

> db.userdata.find({gender: 'male', $or: [{nationality: 'South Africa'},{nationality: 'England'}]}).sort({name: 1}).pretty()
{
        "_id" : ObjectId("56d0aecbc806f4f7cfae1604"),
        "name" : "Kevin",
        "surname" : "Dean",
        "username" : "kevind",
        "nationality" : "England",
        "occupation" : "Developer",
        "gender" : "male",
        "age" : "1992/04/05",
        "os" : "linux"
}
{
        "_id" : ObjectId("56d0ae9cc806f4f7cfae1601"),
        "name" : "Ruan",
        "surname" : "Bekker",
        "username" : "ruanb",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "male",
        "age" : "1987/01/01",
        "os" : "linux"
}

Sorting by nationality in Descending order and name in Ascending order:

> db.userdata.find({gender: 'male', $or: [{nationality: 'South Africa'},{nationality: 'England'}]}).sort({nationality: -1, name: 1}).pretty()
{
        "_id" : ObjectId("56d0b6f1c806f4f7cfae1608"),
        "name" : "Mark",
        "surname" : "Beck",
        "username" : "markb",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "male",
        "age" : "1989/04/05",
        "os" : "mac"
}
{
        "_id" : ObjectId("56d0ae9cc806f4f7cfae1601"),
        "name" : "Ruan",
        "surname" : "Bekker",
        "username" : "ruanb",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "male",
        "age" : "1987/01/01",
        "os" : "linux"
}
{
        "_id" : ObjectId("56d0aecbc806f4f7cfae1604"),
        "name" : "Kevin",
        "surname" : "Dean",
        "username" : "kevind",
        "nationality" : "England",
        "occupation" : "Developer",
        "gender" : "male",
        "age" : "1992/04/05",
        "os" : "linux"
}

Limit Records:

Show only the first 2 records:

> db.userdata.find({nationality: 'South Africa'}).limit(2).pretty()
{
        "_id" : ObjectId("56d0ae9cc806f4f7cfae1601"),
        "name" : "Ruan",
        "surname" : "Bekker",
        "username" : "ruanb",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "male",
        "age" : "1987/01/01",
        "os" : "linux"
}
{
        "_id" : ObjectId("56d0aecbc806f4f7cfae1603"),
        "name" : "Marge",
        "surname" : "Adam",
        "username" : "margea",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "female",
        "age" : "1989/04/05",
        "os" : "mac"
}

To see the 3rd and 4th record, meaning skipping the first 2 records:

> db.userdata.find({nationality: 'South Africa'}).limit(2).skip(2).pretty()
{
        "_id" : ObjectId("56d0aecbc806f4f7cfae1606"),
        "name" : "Marge",
        "surname" : "Adam",
        "username" : "margea",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "female",
        "age" : "1989/04/05",
        "os" : "mac"
}
{
        "_id" : ObjectId("56d0b6f1c806f4f7cfae1608"),
        "name" : "Mark",
        "surname" : "Beck",
        "username" : "markb",
        "nationality" : "South Africa",
        "occupation" : "Sysadmin",
        "gender" : "male",
        "age" : "1989/04/05",
        "os" : "mac"
}

Updating Records:

Mark Beck relocated to Seattle, lets update the record:

> db.userdata.update({username: 'markb'}, {$set: {nationality: 'Seattle'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Deleting Records:

> db.userdata.remove({username: 'markb'})
WriteResult({ "nRemoved" : 1 })

> db.userdata.remove({name: 'Ruan'},{surname: 'Bekker'})
WriteResult({ "nRemoved" : 1 })