Mongodbclass 1
Mongodbclass 1
Class
Type
Materials
Reviewed
NoSQL
NoSQL stands for Not only SQL.
NoSQL is a category of databases, which stores data in non-relational fashion i.e.
NoSQL generally doesn’t store data in tables. The name NoSQL is given because in
most of the NoSQL databases, we don’t write SQL like queries for inserting, deleting,
updating, and fetching data.
Examples of NoSQL: MongoDB, CouchBase, Cassandra, ReThinkDB etc.
JSON
JSON stands for Javascript object notation.
Untitled 1
It is no where actually related to JS. That means if you make a JSON file, JS cant do
much with it.
MongoDB
Links: https://www.mongodb.com/json-and-bson
MongoDB is a document based database, which internally stores data in the form of
BSON, but we as normal developers can send or receive data in form of JSON,
internally mongodb manages the conversion of JSON-BSON and BSON-JSON
automatically.
Now when we used to store data in any RDBMS say MySQL, data was stored in tables.
Tables used to represent real life entity. Inside a table, we had many rows. Rows used
to represent one data record. Columns inside table used to represent properties of the
entity.
Now in mongodb, we store data in form of documents (JSON like).
So here, a real life entity is represented by Collections. What table is for RDBMS is
collections for mongodb. In simple terms, collections are group of JSON documents.
One record in a collection is called as Document. What row is for RDBMS is document
for mongodb.
A document is nothing but a JSON (internally BSON), a JSON has multiple key-value
pairs. The key of JSON represent the property of the entity. So what column is for
RDBMS, key (from key-value pair) is for mongodb .
Installing MongoDB:
Link: https://medium.com/@LondonAppBrewery/how-to-download-install-mongodb-on-
windows-4ee4b3493514
Using MongoDB
Open MongoDB in terminal:
Write mongosh in your terminal, and it will open the mongodb console.
Untitled 2
You can also use MongoDB Compass software, to open the same terminal directly.
show databases;
show dbs;
Untitled 3
How to select a particular DB to work on?
To select a particular db and start querying on it we can use
use name_of_database;
show collections;
Untitled 4
Make sure, we run this command after executing use some_db otherwise we wont be
having db selected.
db.collectionname.find();
Untitled 5
use new_database_name;
The use command creates a new database if there is no already present db with the
same name, otherwise if there is a db with the same name, it just selects it.
Now what will happen is, after creating a DB, if we try to do show dbs; then it will not list
our newly created database. Because, if mondodb sees that there is no valid collection
added in the database, and the db is empty, it doesn’t list it.
Untitled 6
How to add a new collections ?
To create a new collection we can do:
db.createCollection("name_of_the_collection")
In normal RDBMS, while create a table, we have to define what will be the column of
the table. Why we are not defining properties of a collection on mongodb ?
This is because, mongodb doesn’t restrict us by any means for defining documents of a
collection. Two documents of the same collection can posses different type of
properties.
Untitled 7
Untitled 8