0% found this document useful (0 votes)
17 views1 page

DBMS Lab 03

Uploaded by

Mansi Munde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

DBMS Lab 03

Uploaded by

Mansi Munde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

show dbs;

use students;
//DOCUMENTS:
//{ "_id" : ObjectId("65425ee24dae169ba3f964c1"), "name" : "GANESH", "age" : 20 }
//{ "_id" : ObjectId("654291025dbfaef1293ed2a1"), "name" : "adi", "age" : 21 }
//{ "_id" : ObjectId("654292245dbfaef1293ed2a2"), "name" : "pawan", "age" : 23 }
//{ "_id" : ObjectId("654292245dbfaef1293ed2a3"), "name" : "om", "age" : 21 }
//{ "_id" : ObjectId("654292245dbfaef1293ed2a4"), "name" : "adi", "age" : 21 }

//CRUD OPERATIONS

1)C : CREATE and INSERT


db.createCollection("students");
show collections;
db.students.insert({"name":"GANESH","age":21});
db.students.find();

2)R : READ
db.students.find({name:"pawan"}); == db.students.find({query})
//Output-->{ "_id" : ObjectId("654292245dbfaef1293ed2a2"), "name" : "pawan", "age" : 23 } //Whole document having name:"pawan"
db.students.find({name:"pawan"},{name:1/0}); == db.students.find({query},{projection})
//Output--> { "_id" : ObjectId("654292245dbfaef1293ed2a2"), "name" : "pawan" } //With "_id"
db.students.find({name:"pawan"},{_id:0,name:1});
//Output-->{ "name" : "pawan" }
db.students.find({age:21}).limit(1); //Returns only one document amongs multiple documents
//Output-->{ "_id" : ObjectId("65425ee24dae169ba3f964c1"), "name" : "GANESH", "age" : 21 }
//another method for same :db.students.findOne({age:21});
db.students.find({age:21}).limit(1).skip(1);//To skip first record
//{ "_id" : ObjectId("654291025dbfaef1293ed2a1"), "name" : "adi", "age" : 21 } //first record = GANESH is skipped

3) U : UPDATE
db.students.update({name:"adi"},{$set:{age:25}}); //AGE of adi becomes 21 to 25
//Output-->{ "_id" : ObjectId("654291025dbfaef1293ed2a1"), "name" : "adi", "age" : 25 }

4) D : DELETE
db.students.remove({name:"GANESH"}); //to delete single document
//Output-->{ "_id" : ObjectId("654291025dbfaef1293ed2a1"), "name" : "yogesh", "age" : 25 }
//{ "_id" : ObjectId("654292245dbfaef1293ed2a2"), "name" : "pawan", "age" : 23 }
//{ "_id" : ObjectId("654292245dbfaef1293ed2a3"), "name" : "om", "age" : 21 }
//{ "_id" : ObjectId("654292245dbfaef1293ed2a4"), "name" : "adi", "age" : 21 }
db.students.drop();//to delete whole collection
//Output-->true
//GANESH

You might also like