A Start To Learn Mongo DB

A Start To Learn Mongo DB



MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. 
MongoDB works on concept of collection and document.


Collections

A Collection is a set of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. 
Collections do not enforce a schema. Documents within a collection can have different fields. 
Typically, all documents in a collection are of similar or related purpose.


Documents

A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.


Mapping Relational Databases to Mongo DB



Collections in MongoDB is equivalent to the tables in RDBMS.Documents in MongoDB is equivalent to the rows in RDBMS.Fields in MongoDB is equivalent to the columns in RDBMS

Fields (key and value pairs) are stored in document, documents are stored in collection and collections are stored in database.





Here we will see how a table in relational database looks in MongoDB. As you see columns are represented as key-value pairs(JSON Format), rows are represented as documents. MongoDB automatically inserts a unique _id(12-byte field) field in every document, this serves as primary key for each document.

Creating a Database

MongoDB use DATABASE_NAME is used to create database. The command will create a new database if it doesn't exist, otherwise it will return the existing database.

Syntax :     use <desired database name>







List the databases in the Mongo DB System

The Command show dbs command will show the default DB’s and the one’s created by the client. However If there are no collections in the DB created then it won’t be shown in the list.
Also to check which database is currently selected then use the command db

Syntax :     show dbs
Syntax :     db






Drop Database

db.dropDatabase() command is used to drop a existing database.

Syntax :     use <desired database name>
                    db.dropDatabase()














Create Collection

In MongoDB db.createCollection(name, options) is used to create collection.
In the command, name is name of collection to be created. Options is a document and is used to specify configuration of collection.
Options parameter is optional, so you need to specify only the name of the collection.

Syntax :    db.createCollection(name,options)








Find Documents within a Collection

We can use the command db.collectionname.find(). The object ID’s would be appended to the each document on it’s own. This would make each document unique.

Syntax :    db.collectionname.find()

Update Documents within a Collection

To update a particular field use the command,db.collectionname.update()










Syntax :    db.collectionname.update({field:’old name’},{$set:{field:’new name’}})

Delete Documents within a Collection

To delete a document use the command, db.collectioname.remove()






Syntax :    db.collectionname.remove({field:’Value’})




Drop Collection

MongoDB's db.collection.drop() is used to drop a collection from the database.

Syntax :    db.collectionname.drop()



Comments

Popular posts from this blog

Building a React CRUD application using MERN stack

Intro To ReactJS