CRUD operations create, read, update, and delete documents as same like in RDBMS (Select and DMLs).
Firstly knows how to create database in mongoDB
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.
Execute simple command : > use TEST
Switch the Database : > use admin
check your currently selected database : > db
List of Databases available with size : > show databases / show dbs <———- But this command is used directly when your authorization is disable. Later to discuss about authorization.
Create Collection
There is two type of collection normal collection and capped collection. Capped collection is auto purge which is used for like a auditing/Oplog later to discuss about oplog which is part of replication. And mongoDB is schema less so when you create a database its highly recommended to create a any single collection, if you not follow this after switching the newly created database to another one your newly created database is flush i.e not visible when you check with ” > show dbs “ command.
Below the syntax of Collection
> db.createCollection(name, options);
> use Test
switched to db Test
db.createCollection(“student”)
> db.createCollection(“Audit”, { capped : true, autoIndexId : true, size : 6142800, max : 10000 } ) <—– Creating a Capped Collection Syntax
You can check the created collection
> show collections
Document Insert
Above mention the command also working but not need to create collection . In MongoDB, you don’t need to create collection. MongoDB creates collection automatically, when you insert some document
Syntax: > db.Collection.insert ({“filed” : “value”});
> use Test
> db.student.insert({ “name” : “Aman”});
> db.student.insert({“name” : “Steve”, “rollno” : 101,”contact” : “99xxxxxxxx”});
> db.student.insert({“name” : “Kate”, “rollno” : 101,”contact” : “98xxxxxxxx”});
> db.student.insert({“name” : “Tarun”, “rollno” : 102,”contact” : “97xxxxxxxx”});
> db.student.insert({“name” : “Dan”, “rollno” : 103,”contact” : “96xxxxxxxx”});
> db.student.insert({“name” : “Tom”, “rollno” : 104,”contact” : “95xxxxxxxx”});
> db.student.insert({“name” : “Mohan”, “rollno” : 105,”contact” : “93xxxxxxxx”});
> db.student.insert({“name” : “Tin”, “rollno” : 104,”contact” : “92xxxxxxxx”});
> db.student.insert({“name” : “Vince”, “rollno” : 105,”contact” : “91xxxxxxxx”});
Document Read
You can just check the details available in collections.
Syntax: db.collection.find();
> db.student.find() <——- It’s showing complete collection
> db.student.findOne() <——– Showing only single record.
> db.student.find().pretty() <——- Display the results in a formatted.
Later to discuss about all opetions like sorting, orderby, less then, greater than etc.
Document Update
In MongoDB update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.
Syntax: db.collection.update(selection_data, update_data)
> db.student.update.({“name” : “Kate”},{$set:{“rollno”: 104}});
> db.student.find();
Document Delete
Deleting a document from collection. remove() clause used for this.
Syntax : >db.collection.remove(DELLETION_DATA)
> db.student.remove({ “rollno” : 104}) <———- Now Its removing the all entries which is belongs to 104
db.student.find()
> db.student.remove({ “rollno” : 105,1}) <——— Now Its removing only the first record which is belongs to 105
db.student.find()
> db.student.remove() <——— Remove all he documents from the collection.
db.student.find()
For More Detail about the CRUD operations: Click Here
Can you give real time example of capped collection . Where we can use it ?
Hi Naresh,
Real time example if you know the mongodb Replicaset check the oplog its a capped collection.
Where we are using as required auto purging data and its work in LIFO mechanism…. like wants a audit from application end. So define a size or number of records as per know the load.
For more detail follow the link : https://docs.mongodb.com/manual/core/capped-collections/
Regards
Vinay