Introduction to MongoDB

Mongo is a popular document store.

What is it?

MongoDB:

hipster-hacker-mongo-wrongo.png

Getting Started

Install mongo, then run the server. There are a lot of options for running a server but for learning the easiest way is probably to first create a folder to hold your data:

$ mkdir ~/mongo-data

then start a server:

$ mongod --dbpath ~/mongo-data --fork --syslog

Forking means the server will run in the background, until you kill the process.

Just starting up a server like this is only for playing around and getting started learning MongoDB. If you are installing mongo on a real server somewhere you had better learn how to secure your installation, or you will get hacked.

This runs a mongo server instance listening on port 27017. Now run the interactive shell:

$ mongo

Inside the shell, try these commands (in order, obviously):

  show dbs

  use hello

  db.people.insert({name: "Abdul", age: 22, likes: ["skiing", "hiking"]})

  db.people.insert({name: "Barbara", age: 20, country: "UK"})

  db.people.insert({name: "Chi", age: 40, country: "CN",
      job: {title: "welder", salary: {currency: "EUR", amount: 50000}}})

  db.people.insert({name: "Danielle", country: "FR",
      likes: ["cooking", "racing", "skiing"]})

  db.people.count()

  db.people.find()

  db.people.find({age: 20})

  db.people.find({age: {$lt: 30}})

  db.people.find({country: {$in: ["CN", "JP", "FR"]}})

  db.people.find({"job.title": "welder"})

  db.people.find({job: {$exists: 1}})

  db.people.find({country: {$exists: 0}})

  db.people.find({age: {$lt: 30}}, {name: 1})

  db.people.find({age: {$lt: 30}}, {_id: 0, name: 1})

  db.people.find({}, {_id: 0, name: 1, country: 1})

  db.people.find({}, {_id: 0, name: 1, country: 1}).sort({name: -1})

  db.people.update({name: "Barbara"}, {$set: {likes: ["hiking"]}})

  db.people.find({likes: "hiking"})

  db.people.distinct("likes")

  db.people.distinct("country")

  db.people.find({}, {_id: 0, name: 1, likes: 1}).forEach(
      function (d) {print(d.name + " " + d.likes)})

  db.people.insert({name: "Etienne", country: "FR", likes: ["scuba"]})

  db.people.update({country: "FR"}, {$addToSet: {likes: "wine"}})

  db.people.find({}, {_id: 0, name: 1, likes: 1}).forEach(
      function (d) {print(d.name + " " + d.likes)})

  db.people.update({country: "FR"}, {$addToSet: {likes: "wine"}}, {multi: true})

  db.people.find({}, {_id: 0, name: 1, likes: 1}).forEach(
      function (d) {print(d.name + " " + d.likes)})

  db.people.count()

  db.people.update({name: "Faye"}, {$set: {age: 3}})

  db.people.count()

  db.people.update({name: "Faye"}, {$set: {age: 3}}, {upsert: true})

  db.people.count()

  db.people.update({name: "Chi"}, {$addToSet: {likes: "swimming"}},
      {upsert: true})

  db.people.update({name: "Abdul"}, {$set: {country: "TN"}})

  db.people.aggregate({$group: {_id: "$country", num_people: {$sum: 1}}})

  db.people.aggregate([
    {$unwind : "$likes"},
    {$group : {_id : "$likes", number: {$sum : 1}}},
    {$sort : {number : -1}},
    {$limit : 5}
  ])

  db.people.aggregate([
    {$unwind : "$likes"},
    {$group : {_id : {$toUpper: "$likes"}, people: {$addToSet : "$name"}}},
    {$sort : {_id : 1}},
    {$limit : 10}
  ])

Congratulations, you've just learned how to:

More Commands

Advantages and Disadvantages of Mongo

Some articles weighing in on the question: