MongoDB Tutorial for Beginners


Gist

Mongo DB is the world's most advanced and popular document oriented database. It was created in 2007 after the team at double click, a company serving 400,000 ads per second faced issues with scalability and flexibility using existing database systems. This inspired them to design a database where all data is stored in JSON like documents which are organized into collections where they can be queried. Unlike a relational table, a predefined schema for a collection is optional which allows you to evolve your data structures rapidly without running complex database migrations.

But more importantly, it allows data that's frequently accessed together by an app to be stored in the same place. This makes read operations extremely fast because no joins are required. It's like having a fully assembled car ready to go as opposed to joining together a bunch of separate parts. And that also makes the database much easier to scale horizontally via shing. Unlike relational tables, collections are self contained, making them much easier to work with in a distributed system.

That's why they call it Mongo. It's designed for humongous workloads. When you create a new document in Mongodb, it's assigned an object ID that's unique to that collection inside the document. You can define multiple fields where the value might be a variety of different data types like a string array object and so on. Now to work with your data in any programming language, the query API is used to perform basic read and write operations as well as complex queries and transformations across the database to optimize performance secondary indexes can be created to make common queries extremely fast.

It also supports geospatial queries to find all the documents near a geographic location. In addition to queries, you can also create data aggregation pipelines which can group documents together and reduce them to a single result. Mongodb is free to use and can be self hosted. But the quickest way to get started is with the free tier on Atlas. It scales automatically and provides a UI where you can interact with your data plus a ton of other awesome features like full tech search that leverages the power of Apache lucine. In addition, they just launch a serverless option where you only pay for the exact resources that you use.

And Atlas has other essential features like triggers that make it easy to run serverless functions. When your underlying data changes,then there's realm which is a fire base like platform that makes it easy to sync up your data with afront and application in real time.

How close to Relational DB

Mongo DB is a non-relational Database. But being a DB resembles many similarities with Relational DB.
- Rows in table -> documents in Collection.
- In relational db there is schema of tablel to maintain but here there is no need of that. There is no constraint of same schema in all rows/docs.



Database Commands(mongo-shell)

View all databases

show dbs


Create a new or switch databases

use dbName


View current database

db


Delete current database

db.dropDatabase()



--now collections commands


Show collections of current database

show collections


Create a collection names comments

db.createCollection('comments')


Delete collection n

db.n.drop()


Insert a Row in collection comments

db.comments.insert({

    'name': 'Krishna',

    'lang': 'JavaScript',

    'member_since': 5

})


Insert many Rows in collection comments

db.comments.insertMany([{

    'name': 'Krishna',

    'lang': 'JavaScript',

    'member_since': 5

},

{

    'name': 'Vasudeva',

    'lang': 'Python',

    'member_since': 8

},

{

    'name': 'Acyuta',

    'lang': 'Java',

    'member_since': 2

}

])


Show all the rows of the collection

db.commets.find()


Find only one entry matching the collection and stop searching

db.comments.findOne({lang:'JavaScript'})


Show everything in a good manner(prettified)

db.comments.find().pretty()


Search in a MongoDB database

db.comments.find({lang:'JavaScript', name:'Krishna'})


Chaining multiple commands

db.comments.find({lang:'JavaScript',name:'Krishna'}).pretty().limit(2)


Limit the number of rows in output

db.comments.find().pretty().limit(4)


Show the total number of rows/objects in the output

db.comments.find().pretty().find() //5


Limit() only limits the content to show not the actual output. It only limits the number of objects to be printed

db.comments.find().limit(2).count() will show 5 not 2


Sorting of output

db.comments.find().sort({member_since:1}).pretty()

db.comments.find().sort({lang:-1})

1 for ascending and -1 for descending


Update a row

db.comments.update({name:'Krishna'},

    {

    'name': 'Krishna',

    'lang': 'JavaScript',

    'member_since': 51

})

also if the multiple enties are present it only updated one entry


MongoDB Increment operator

db.comments.update({name:'Krishna'},

    {

        $inc:{

            member_since: 2

        }

    }

)


MongoDB rename operator

db.comments.update({name:'Krishna'},

    {

        $rename:{

            member_since: 'member'

        }

    }

)


Delete Row

db.comments.remove({name:'Krishna'})

//and its case sensetive it respects whether i'm specifying capital or small data


Less than

db.comments.fins({member_since:{$lt:66}})


Greater than

db.comments.fins({member_since:{$gt:6}})

 

Next Post Previous Post
No Comment
Add Comment
comment url