0% found this document useful (0 votes)
7 views

mongodb_lab_2

Uploaded by

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

mongodb_lab_2

Uploaded by

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

MongoDB LAB - Managing NOTES and IMAGES Collections

Part I: Managing 'NOTES' Data

1. Import data from notes.json into the NOTES collection


Command:

mongoimport --db STUDENTS --collection NOTES --file notes.json --jsonArray

2. Display the number of documents in the NOTES collection


Command:

db.NOTES.countDocuments();

3. Display the number of scores assigned for exams


Command:

db.NOTES.countDocuments({ "type": "exam" });

4. List students with scores below 50 in quizzes


Command:

db.NOTES.find({ "type": "quiz", "score": { $lt: 50 } });

5. Display student IDs and homework scores, sorted by score


Command:

db.NOTES.find({ "type": "homework" }, { "student_id": 1, "score": 1, "_id": 0 }).sort({ "score":


1 });

6. Add the following documents to the NOTES collection


Command:

db.NOTES.insertMany([
{ "student_id": 200, "type": "exam", "score": 39.15737165675101 },
{ "student_id": 200, "type": "quiz", "score": 64.28777986675444 },
{ "student_id": 200, "type": "homework", "score": 17.15638165456321 },
{ "student_id": 200, "type": "homework", "score": 85.23345595675101 }
]);

7. Update the exam score of student 200 to 59


Command:

db.NOTES.updateOne(
{ "student_id": 200, "type": "exam" },
{ $set: { "score": 59 } }
);

8. Set the same score (44) for all tests of student 200
Command:

db.NOTES.updateMany(
{ "student_id": 200 },
{ $set: { "score": 44 } }
);

9. Increment the student ID of student 200 by 2


Command:

db.NOTES.updateMany(
{ "student_id": 200 },
{ $inc: { "student_id": 2 } }
);

10. Delete all documents of student 202


Command:

db.NOTES.deleteMany({ "student_id": 202 });

Part II: Managing 'IMAGES' Data

1. Import data from images.json into the IMAGES collection


Command:

mongoimport --db STUDENTS --collection IMAGES --file images.json --jsonArray

2. Insert a new image


Command:

db.IMAGES.insertOne({
"_id": 100000,
"height": 480,
"width": 640,
"tags": ["cats", "dogs", "kittens"]
});

3. Add the tag ‘vacation’ to the image with ID 9


Command:

db.IMAGES.updateOne(
{ "_id": 9 },
{ $push: { "tags": "vacation" } }
);

4. Change the first tag of image 7 to ‘cats’


Command:

db.IMAGES.updateOne(
{ "_id": 7 },
{ $set: { "tags.0": "cats" } }
);

5. Remove the tag ‘work’ from the image with ID 99977


Command:

db.IMAGES.updateOne(
{ "_id": 99977 },
{ $pull: { "tags": "work" } }
);

You might also like