MongoDB in 10 minutes

humongous

MongoDB is one of the most trending NoSQL databases. It is a scalable document-oriented DB and it stores data in JSON files. Comparing to relational databases’ tables Mongo has collections. Something MongoDB doesn’t have are transactions and relations. It is schemaless, has a JavaScript console and provides some agility for software development phases. High availability of its data is provided by the replication feature.
mongodb in 10 minutes

Use this official Installation guide. When you have database installed just execute command “mongo” in console.

 

CRUD-operations

This is a tutorial for learning the basics of MongoDB in 10 minutes. Let’s insert some data. To start with, specify which database do you want to use (If it doesn’t exist yet it would be created):

use vehicles

The insertion goes in such format: db.<collection_name>.insert({<your_json_here}), where you should specify a collection’s name and a JSON-object to insert.

db.cars.insert(
    {
        “model” : ”BMW X6”,
        “year” : ”2015”,
        “owner” : {
            “name” : ”John Doe”
         }
    }
)

Try to add another cars with some fields.

To retrieve all data from MongoDB collection:

db.cars.find();

Here you can see our records with an advanced field “_id”. This is an ID that Mongo provides for each record.

To retrieve specific data use same command but with a JSON query object {field1 : value1, field2 : value2, … , fieldN : valueN}:

db.cars.find({“model”:”BMW X6”})

To update a document specify 2 JSONs:

– a query to select documents that you want to update,

– an updated field started from $set keyword:

db.cars.update(
    {“model” : “BMW X6”},
    {
         $set : {“year” : ”2016”}
    }
)

To delete data use method remove() with specified query to find documents for removing:

db.cars.remove()

Pros:

  • simple data replication
  • agile MapReduce data aggregation
  • quicker than SQL
  • schemaless (when data structures can be changed often)
  • geographic indexes

Do not use for:

  • banks, billing systems (and similar systems that need high stability and transactions)
  • comprehensive systems that needs a lot of complicated relationships.

Leave a Reply

Be the First to Comment!