Mongo Basic

NoSql– means not only sql. In this we are not going to use structured query language. It is different from relational database.

Why is need of NoSQL?

With the increase of internet usage the companies like google, amzon, ebay, facebook, twitter they get 2.5 quintillion byes of data. Referring url

So it required to handle such big-data big database and thedata which is generated is in all forms like structured, unstructured or semi-structuredso in rdbms it only handle structured data so how to handle semi andun-structured data that’s why there is need of NoSQL database. RDBMS failed tohandle to large amount data.

Features of NoSQL database:

  1. It stores large volumes of structured,unstructured and semi structured data.
  2. High performance, high scalability and highavailability.
  3. It provide quick iterations
  4. We can use object oriented programming in thesedatabases.
  5. The support replication
  6. Auto-sharding – it means it converts large databaseinto small, fast or easily manageable parts or chunks. Meaning of shard meanssmall parts.
  7. Also support dynamic schemas.

Types on NoSQL databases: –

  1. Key-Value Stores: Redis, dynamo
  2. GraphDb- orientDB, Neo4j
  3. Column Oriented- BigTable, Cassandra
  4. Document Db- mongoDb and CouchDb

Difference between RDBMS and MongoDB

In RDBMS we have server àon which Database present àDatabase contains Tables àtable contains rows. Every table in database tables have fixed schema meanstable structure. We use table to write complex joins and if table are big joinsare expensive.

In MongoDb we have serveràon which database present àdatabase contains collectionsàin collections contains documents àin document contains fields. In this collection is schema. Means no need todecide which kind of data enter into that document.

To start MongoDb go to installation directory and use runmongo.exe from command prompt.

C:\ProgramFiles\MongoDB\Server\4.0\bin > this is path of installation directorywhich is default.

Then run Mongo.exe to start server.     

  1. Show dbs:  this command is used to show which database present on server.
  2. Use dbName: this command is used for if the database is present it switched to that database but If not present then it will create new database of that name which is provided in this command. After using use command but you can’t see a database is created for this you need to create one collection and add at least one row to that collection.
  3. To create collection use below command :
    1. db.departments.insert({Id:1,name:’Hr’})
  4. createCollecion: db.createCollection(“departmentDemo”) both command are used to create collection but first command create collection and insert one row in that collection and in second command you can create empty collection. In this command createCollection will take 2 parameters first is collection name and second is Options.
    • Name: is type of string and description is name of that collection
    • Options: is a document and description is used to specify indexing, memory size and maximum number of documents.  This is completely optional parameter.
      • i.Capped: it is collection which deletes old data automatically when it reaches it maximum size and it required size parameter also.
      • Max: max documents which enter.
    • db.createCollection(“employee”,{capped:true,size:3644,max:1000})
  5. Show collections: This command is used for to show the collection which are present in that respective database.
  6. Find: Is like select query  db.departments.find() this is used to select all fields with all documents in that collection.
  7. Pretty: db.departments.find().pretty() this command is used to better format of output of that command or find command which  executed.
  8. explain: this is used for explaining the query which you want explanations. db.departments.find().pretty().explain()
  9. insert: is used to insert document in collection db.departments.insert({Id:1,name:’Hr’})
  10. Save: one another method is to insert document in collection in below example we create one anonymous object and assign data to that properties and save that data into collection by using save method.

> var empinfo={};

> empinfo.Name=”sagar jaybhay”;

sagar jaybhay

> empinfo.salary=11000;

11000

> empinfo.department=”development”;

Development

empinfo.languages=[“Hindi”,”English”,”Marathi”];

> db.employee.save(empinfo);

db.employee.find().pretty()

  1. Drop database: to drop database you need to switch that database first by using use database name command and then use db.dropDatabase() to drop a database. Commands are case sensitive so write commands as it is.
  2. Drop Collection: db.collectionname.drop() this is the command without parameter. It is parameter less method and return true or false based on successful delete.
    1. db.employee.drop();

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *