NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

use dbname
show dbs

db.createCollection("student")
show collections
db.student.drop()

db.student.insert({
"name": "Sowmyasree",
});

db.student.insert({
"name": "Sowmyasree",
"lastname":"Vanukuru"
});

db.student.find()
db.student.find().pretty()

db.student.insert({
"name": "Chirag",
"lastname":"Mahida",
"city":"Surat"
});
db.student.insert({
"name": "Ria",
"lastname":"Soni",
"city":"Valsad"
});
db.student.insert({
"name": "Dhruvin",
"lastname":"Modi",
"city":"Vapi"
});


------------------------------
Practical List 1
use Mongo_demo
Students(enro, fname, lname, city, gender, contactno, dob)

db.createCollection("Students")
show collections

db.Students.insertMany([
{"enro": 4,"Fname":"Isha","Lname":"Motiyani","City":"Surat","Gender":"F","contactno":9874563210,"dob":"12/09/2000"},
{"enro": 102,"Fname":"Anish","Lname":"Patel","City":"Valsad","Gender":"M","contactno":9632587410,"dob":"06/01/2001"},
{"enro": 103,"Fname":"Kalp","Lname":"Shah","City":"Baroda","Gender":"M","contactno":9630125874,"dob":"13/10/2000"},
{"enro": 104,"Fname":"Yash","Lname":"Bhakta","City":"Mumbai","Gender":"M","contactno":9987452103,"dob":"08/05/2001"},
{"enro": 84,"Fname":"Aparana","Lname":"Mukherjee","City":"Surat","Gender":"F","contactno":8563247890,"dob":"26/06/2001"},
{"enro": 8,"Fname":"Arjun","Lname":"Poduval","City":"Vapi","Gender":"M","contactno":8965230147,"dob":"01/05/2001"},
{"enro": 105,"Fname":"Riya","Lname":"Agrawal","City":"Navsari","Gender":"F","contactno":6985321470,"dob":"02/09/2000"},
{"enro": 79,"Fname":"Monis","Lname":"Khan","City":"Bardoli","Gender":"M","contactno":7895231053,"dob":"23/11/2000"},
{"enro": 106,"Fname":"Abhishek","Lname":"Choksi","City":"Surat","Gender":"M","contactno":6985327410,"dob":"18/07/2001"},
{"enro": 49,"Fname":"Om","Lname":"Patel","City":"Valsad","Gender":"M","contactno":9985632100,"dob":"15/05/2001"},
]);

1.
db.Students.find().pretty();

2.Show only the first name and last name of all the students.
db.Students.find({ }, { "Fname" : 1 ,"Lname" : 1 , _id:0 } );

3.Display the list of unique cities of the students.
db.Students.distinct("City");

4.Retrieve the student‟s date of birth along with the student first name and last name.
db.Students.find({ }, { "dob":1,"Fname" : 1 ,"Lname" : 1 , _id:0 } );

5.Get all the details of students whose city is “Surat”.
db.Students.find({ "City" : "Surat"},{}).pretty();

6.Arrange all the documents of students in ascending order using “enro” number.
db.Students.find().sort({"enro" : -1 }).pretty()

7.Show the details of just the first five documents of the students.
db.Students.find().limit(5)

8.Display only the details of last three documents of the students.
db.Students.find().skip(db.Students.count()-3)

9.View the list of male students whose city is “Valsad”.
db.Students.find({ "City" : "Valsad","Gender":"M"},{}).pretty();

10.Display all the documents in descending order.
db.Students.find().sort({"enro" : 1 }).pretty()

11.Display the list of first four students whose city is “Valsad” in order.
db.Students.find({"City":"Valsad"}).limit(4)

12.Update the contact number and date of birth of “Monis” to “6852987530” and “06/01/1999” respectively.
db.Students.update( { "Fname" : "Monis" },{ $set: {"contactno" : 6852987530 , "dob" : "06/01/1999"} } );

13.Find only the date of birth of “Om”.
db.Students.find({"Fname":"Om"},{"Fname":1,"dob":1,_id:0});

14.Insert the document (100, Rohit, Mehera, Mumbai, 24/03/2003) in the collection.
db.Students.insertOne({"enro": 100,"Fname":"Rohit","Lname":"Mehera","City":"Mumbai","dob":"24/03/2003"})

15.Display the list of male students whose last name is “Patel”.
db.Students.find({"Lname":"Patel","Gender":"M"},{_id:0});

16.Insert the document (35, Priyank, Mistry, Anand, 18/06/2000, 9000800001) in the collection.
db.Students.insertOne({"enro": 35,"Fname":"Priyank","Lname":"Mistry","City":"Anand","dob":"18/06/2000","contactno":9000800001})

17.Show all the details of student whose enrollment number is “102”.
db.Students.find({"enro":102},{}).pretty();

18.Display the list of students whose gender in “NULL” or 0.
db.Students.find({$or:[{"Gender":"NULL"},{"Gender":0}]}).pretty();

19.Complete all details of students, if any document of student the document is incomplete.
db.Students.update( { Fname : "Rohit" },{ $set: {"Gender":"M","contactno":8200950913 } } );
db.Students.update( { Fname : "Priyank" },{ $set: {"Gender":"M" } } );

20.Remove all the details of students whose city is “Vapi”.
db.Students.remove({"City":"Vapi"})

21.Display the next five students after skipping first five which are in the collection.
db.Students.find().skip(5).limit(5).pretty();

22.List the names of all the students having "a" as the second letter in their names.
db.Students.find({"Fname":{$regex: "^.a"}},{_id:0})

23.List the clients who stay in a city Bardoli, whose first letter is "M".
db.Students.find({"City":"Bardoli","Fname":{$regex: "^M"}},{_id:0})

24.Count number of students in the collection.
db.Students.count()

25.Make the collection empty
db.Students.remove({})

----------------------------------------------------------------------------
Practical List 2

use bmiit2022

db.createCollection("Items")

db.Items.insertMany([
{"id":"TP01","name":"Talcom Powder","manufracturer":"Persona","price":40,"qty":45,"stockdate":"11-02-18","dealer":"Krishna Cosmetik"},
{"id":"FW05","name":"Face Wash","manufracturer":"Attitude","price":45,"qty":55,"stockdate":"15-04-18","dealer":"Surbhi Collection"},
{"id":"BS01","name":"Bath Soap","manufracturer":"Persona","price":40,"qty":45,"stockdate":"11-02-18","dealer":"Krishna Cosmetik"},
{"id":"SH06","name":"Shampoo","manufracturer":"Shampoo","price":12,"qty":50,"stockdate":"05-01-17","dealer":"Surbhi Collection"},
{"id":"FW12","name":"Face Wash","manufracturer":"Loreal","price":95,"qty":45,"stockdate":"05-01-16","dealer":"Surbhi Collection"},
{"id":"S5005","name":"Ball Pen 0.5","manufracturer":"Nataraj Classic","price":16,"qty":100,"stockdate":"31-03-10","dealer":"Clear Deals"},
{"id":"S5003","name":"Ball Pen 0.25","manufracturer":"Nataraj Classic","price":20,"qty":150,"stockdate":"01-01-10","dealer":"Clear Deals"},
{"id":"S5002","name":"Gel Pen Premium","manufracturer":"Pilot Juice","price":14,"qty":125,"stockdate":"14-02-10","dealer":"Reliable Stationers"},
{"id":"S5006","name":"Gel Pen Classic","manufracturer":"Pilot Juice","price":22,"qty":200,"stockdate":"01-01-10","dealer":"Reliable Stationers"},
{"id":"S5001","name":"Eraser Small","manufracturer":"Apsara","price":15,"qty":210,"stockdate":"19-03-09","dealer":"Clear Deals"},
{"id":"S5004","name":"Eraser Big","manufracturer":"Apsara","price":10,"qty":60,"stockdate":"12-12-09","dealer":"Clear Deals"},
{"id":"S5009","name":"Sharper Classic","manufracturer":"Nataraj Classic","price":18,"qty":160,"stockdate":"23-01-09","dealer":"Clear Plastics"},
])

db.Items.find().pretty();

1.Display only the name and price of items, whose manufacturer is “Persona”.
db.Items.find({"manufracturer":"Persona" }, { "name" : 1 ,"price" : 1 , _id:0 } );

2.Count the total number of manufacturers.
db.Items.distinct("manufracturer").length

3.Display all the item’s details, whose names starts from "Sh".
db.Items.find({"name":{$regex: "^Sh"}},{_id:0})

4.Display the items details of all different types of pens.
db.Items.find({"name":{$regex: ".Pen."}},{_id:0})

5.Display only the name and price of product along with quantity, whose price is more than 80
db.Items.find({"price":{$gt:80}},{"name":1,"price":1,_id:0}).pretty()

6.Show only the item and its price of those items, whose price is less equal to 20.
db.Items.find({"price":{$lte:20}},{"name":1,"price":1,_id:0}).pretty()

7.Display the details of those items; whose dealer code is 102 or quantity is more than 100.
db.Items.find({$or: [{"id":"102"},{"qty":{$gt:100}}]})

8.Show the details of items, whose quantity is in the range of 50 to 100.
db.Items.find({$and:[{"qty":{$gt:50}},{"qty":{$lt:100}}]})

9.Increase the price of items by 10.
db.Items.updateMany({},{$inc:{"price":10}});

10.Decrease the quantity of items by 30.
db.Items.updateMany({},{$inc:{"qty":-30}});

11.Update the price of items by increasing 15%.(1+15/100)
db.Items.updateMany({},{$mul:{"price":1.15}});

12.Find the maximum and minimum price of item in a single query.
db.Items.aggregate({$group:{_id:0,min:{$min:'$price'},max:{$max:'$price'}}});

13.Calculate the average of item price.
db.Items.aggregate({$group:{_id:0,avg_price:{$avg:'$price'}}});

14.Calculate the total amount of items and find the average of it.
db.Items.aggregate({$group:{_id:0,total_price:{$sum:'$price'},avg_price:{$avg:'$price'}}})

15.Display all item names and its price in following format: Talcom Powder price is 40. [Hint: $concat]
db.Items.aggregate({$project:{_id:0,msg:{$concat:["$name","price is ",{ $toString: "$price" }]}}})
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.