Basic Select and Insert Queries


OPEN MONGOSH


Double-click on the mongosh.exe


Mongosh

VIEW ALL DATABASES


you can use the show command with the dbs option to display the list of databases. Type "show dbs" and press Enter


Mongosh

CREATE NEW DATABASE


Create new database using the use command followed by the desired database name. For example, to switch to a new database named "school", you can type "use school":

use school

If the "school" database already exists, "mongosh" will switch to that database. If the "school" database doesn't exist, "mongosh" will create it and switch to it.


Mongosh

HOW TO INSERT NEW RECORDS


You can then start inserting data into the new "school" database. For example, you can use the insertOne commands to insert documents into collections within the "school" database. Here's an example of inserting a document into a "students" collection in the "school" database:

db.students.insertOne({"name":"Joes","age":30,"city":"Salem"})

This will insert a document with the fields "name" , "age" and "city" into the "students" collection in the "school" database. If the "school" database does not already exist, MongoDB will automatically create it for you.


Mongosh

HOW TO SELECT RECORDS


Use the db.collection_name.find() method to query data from a specific collection. For example, if you have a collection named students, you can use the following command to find all documents in the students collection:

db.students.find()

This will return all documents in the students collection.


Mongosh

HOW TO INSERT ANOTHER NEW RECORDS


You can then start inserting data into the new "school" database. For example, you can use the insertOne commands to insert documents into collections within the "school" database. Here's an example of inserting a document into a "students" collection in the "school" database:

db.students.insertOne({"name":"Ram","age":25,"city":"Chennai","contact":"9876543210"});

This will insert a document with the fields "name" , "age", "city" and "contact" into the "students" collection in the "school" database. If the "school" database does not already exist, MongoDB will automatically create it for you.


Mongosh

HOW TO SELECT RECORDS


Use the db.collection_name.find() method to query data from a specific collection. For example, if you have a collection named students, you can use the following command to find all documents in the students collection:

db.students.find()

This will return all documents in the students collection.


Mongosh

HOW TO INSERT ANOTHER NEW RECORDS


You can then start inserting data into the new "school" database. For example, you can use the insertOne commands to insert documents into collections within the "school" database. Here's an example of inserting a document into a "students" collection in the "school" database:

db.students.insertOne({"name":"Sam","age":20,"city":"Chennai","contact":9885967895,"address":{"street":"Cherry Road","pincode":636007}});

This is the document (object) that you want to insert into the collection. It contains multiple fields such as name, age, city, contact, and a nested subdocument address with fields street and pincode.


Mongosh

HOW TO SELECT RECORDS


Use the db.collection_name.find() method to query data from a specific collection. For example, if you have a collection named students, you can use the following command to find all documents in the students collection:

db.students.find()

This will return all documents in the students collection.


Mongosh