Basic Queries for Creating, Updating, and Deleting Database Components

By | May 4, 2023

In this article we are going to learn a few basic queries for Creating, Updating and Deleting the database components.

Learning Queries to play around with the database:

We will classify the queries into 2 groups:

  1. Queries to handle the database hierarchy.
  2. Queries to search and retrieve records using some search parameter.

 

Queries to handle the database hierarchy:

Creating a new database To create a new database, you can use the command

use db_name

The same command is also used to switch between the available db’s. To list the available db’s:

show dbs

NOTE: A newly created database won’t be listed unless you insert a new record into the DB.

Drop database To drop a database, use the command,

db.dropDatabase()

This will drop the current database along with all the collections and records present in the database. So before dropping another database a user needs to switch to that particular database before deleting the same.

Creating a Collection Switch to the DB where you want to create a new Collection, then you can run the below command

db.createCollection("your_collection_name")

Listing available collections To list all the available collections inside a database:

show collections

Listing all the records present in a collection To see all records available in a single collection you can use the below command,

db.collection_name.find()

Rename collection To change the name of an existing collection,

db['curent_name_of_the_collection'].renameCollection('new_name_of_the_collection')

Drop collection To drop a collection along with all the records inside it:

db.collection_name.drop()

Inserting a new document ( JSON object) into the collection If I want to insert a JSON object with details as given in the example below into a collection named Test the query would be like this,

db.collection_name.insert({JSON_object})
Ex: db.Test.insert({"First name"  : Mithlesh, "Last name" : "Thiyari"})

Remove document To remove a particular document from some collection, we need to give the Identity of the document enclosed within the parenthesis.

db.collection_name.remove({'Identity of document which needs to be removed'})
Ex: db.Test.remove({"First name":"Mithlesh", "Last name": "Thiyari"})

Remove all documents This command deletes all the documents present inside a Collection making the collection empty.

db.collection_name.remove()
Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.