WorksheetsMLSC MongoDB Quiz
Total questions: 18
Worksheet time: 14mins
Which numeric type is a valid MongoDB BSON type?
Float
Number
BIGINT
32-bit integer
Given the following documents in a collection:
{ id: 1, n: [1,2,5], p: 0.75, c: 'Green' },
{ id: 2, n: 'Orange', p: 'Blue', c: 42, q: 14 },
{ _id: 3, n: [1,3,7], p: 0.85, c: 'Orange' }
{ _id: 1, n: [1,2,5], p: 0.75, c: 'Green' }
{ _id: 5, n: [1,2,5], p: 0.75, c: 'Green' }
{ _id: 2, n: [1,2,5], p: 0.75, c: 'Green' }
{ _id: 6, n: [1,3,7], p: 0.85, c: 'Orange }
Given the following documents in a collection:
{_id: 1, txt: "just some text"},
{_id: 2, txt: "just some text"}
{_id: 0, txt: "just some text"}
{_id: 1, txt: "just some text"}
{_id: [4], txt: "just some text"}
{_id: 3, txt: "just some text"}
Given the following document:
The name is a.log, the owner of the file is applicationA, the size of the file is 1KB, and the file was deleted.
What command will properly add this document to the files collection using mongosh?
db.files.insertOne({ file: "a.log", owner: "applicationA", size: 1KB, deleted: true })
db.files.insertOne({ file: "a.log", owner: "applicationA", size: 1KB, deleted: True })
db.files.insertOne({ file: "a.log", owner: "applicationA", size: 1024, deleted: true })
db.files.insertOne({ file: "a.log", owner: "applicationA", size: 1024, deleted: True })
Given the following sample documents in products collection:
{ "name" : "XPhone", "price" : 799, "color" : [ "white", "black" ], "storage" : [ 64, 128, 256 ] },
{ "name" : "XPad", "price" : 899, "color" : [ "white", "black", "purple" ], "storage" : [ 128, 256, 512 ] },
{ "name" : "GTablet", "price" : 899, "color" : [ "blue" ], "storage" : [ 16, 64, 128 ] },
{ "name" : "GPad", "price" : 699, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256, 1024 ] },
{ "name" : "GPhone", "price" : 599, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256, 512 ] }
Given the following query:
db.products.find({$and : [{"price" : {$lte : 800}}, {$or : [{"color" : "purple"}, {"storage" : 1024}]}]})
{ "name" : "XPhone", "price" : 799, "color" : [ "white", "black" ], "storage" : [ 64, 128, 256 ] }
{ "name" : "XPad", "price" : 899, "color" : [ "white", "black", "purple" ], "storage" : [ 128, 256, 512 ] }
{ "name" : "GPhone", "price" : 599, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256, 512 ] }
{ "name" : "GPad", "price" : 699, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256, 1024 ] }
An `inventory` collection consists of 200 documents.
What method should be used to get all documents from a cursor using mongosh?
db.inventory.findOne()
db.inventory.find().toArray();
db.inventory.find();
db.inventory.findMany().toArray()
Given the data set and query:
{ "_id" : 1, "player" : "p1", "score" : 89 }
{ "_id" : 2, "player" : "p2", "score" : 85 }
{ "_id" : 3, "player" : "p2", "score" : 65 }
{ "_id" : 4, "player" : "p3", "score" : 65 }
{ "_id" : 5, "player" : "p3", "score" : 75 }
{ "_id" : 6, "player" : "p5", "score" : 70 }
{ "_id" : 7, "player" : "p6", "score" : 100 }
Query : db.scores.aggregate( [{ $group: { _id: '$player', score: { $avg: '$score' } }, { $match: { score: { $gt: 70 } } ])
What is the output?
{ "player" : "p1", "score" : 89 } { "player" : "p2", "score" : 85 } { "player" : "p3", "score" : 75 } { "player" : "p6", "score" : 100 }
{ "player" : "p1", "score" : 89 } { "player" : "p2", "score" : 75 } { "player" : "p6", "score" : 100 }
{ "player" : "p1", "score" : 89 } { "player" : "p2", "score" : 75 } { "player" : "p3", "score" : 70 } { "player" : "p5", "score" : 70 } { "player" : "p6", "score" : 100 }
{ "player" : "p1", "score" : 89 } { "player" : "p2", "score" : 75 } { "player" : "p3", "score" : 70 } { "player" : "p6", "score" : 100 }
A collection coll in database mdb has the following documents :
{_id: 1, type: "A", value: 60}
{_id: 2, type: "B", value: 80}
{_id: 3, type: "C", value: 10}
Query : db.getSiblingDB("mdb").coll.aggregate([ { $out: {db:'test', collection:'results'}} ])
What are two expected results?
Collection `results` is created in database `test`.
There is a syntax error command. Collection `results` is not created.
No documents in collection `coll` are written to collection `results`.
All documents in collection `coll` are written to collection `results`.
Given the following documents:
{_id:1, a: "one", b: "four"}
{_id:2, a: "two", b: "four"}
{_id:3, a: "three", b: "four", c: "three"}
Query : db.coll.replaceOne({}, {a: "ten", b: "five"})
What is the result?
{_id:1, a: "ten", b: "five"} {_id:2, a: "ten", b: "five"} {_id:3, a: "ten", b: "five"}
{_id:1, a: "ten", b: "five"} {_id:2, a: "two", b: "four"} {_id:3, a: "three", b: "four", c: "three"}
{_id:1, a: "ten", b: "five"} {_id:2, a: "ten", b: "five"} {_id:3, a: "ten", b: "five", c: "three"}
{_id:1, a: "one", b: "four"} {_id:2, a: "two", b: "four"} {_id:3, a: "three", b: "four", c: "three"}
Given the collection called coll, with only the following documents,
{ id:1, a:1, b:1 }, { id:2, a:2 }
The update operation db.coll.updateMany({},{$set:{b:2}}) successfully completes.
What is the output of db.coll.find()?
[{_id:1, b:2}, {_id:2, b:2}]
[{_id:1, a:1, b:2}, {_id:2, a:2}]
[{_id:1, a:1, b:1}, {_id:2, a:2, b:2}]
[{_id:1, a:1, b:2}, {_id:2, a:2, b:2}]
Given the following document from the cakeFlavors collection. All documents in this collection have the same schema.
{ "_id" : 1, "flavor" : "chocolate", "number" : 15 }
What operation on the cakeFlavors collection will update the value of the number field to 100 for a document with a "strawberry" flavor value and insert a new document if it does not exist?
db.cakeFlavors.updateOne({ flavor: "strawberry"} , { $set: { number: 100 } }, { $upsert: true })
db.cakeFlavors.insertOne({ flavor: "strawberry"} , { $set: { number: 100 } }, { $upsert: true })
db.cakeFlavors.insertOne({ flavor: "strawberry"} , { $set: { number: 100 } }, { upsert: true })
db.cakeFlavors.updateOne({ flavor: "strawberry"} , { $set: { number: 100 } }, { upsert: true })
Given the following example document from the movie collection:
{ _id: 1, genres: [ "Drama", "Romance", "War" ], title: "A.B.", year: 1921, tomatoes: { rating: 3.9, votes: 507, id: "76" }, countries: [ "USA" ], classic : false }
All documents in this collection have the same schema.
What command updates the value of the classic field to true for all documents with a year value less than 2000?
db.movie.updateOne({ year: { $lt: 2000 } }, { $set: { classic: true } }, { multi: true })
db.movie.updateMany({ year: { $lt: 2000 } }, { $set: { classic: true } })
db.movie.updateMulti({ year: { $lt: 2000 } }, { $set: { classic: true } })
db.movie.updateBulk({ year: { $lt: 2000 } }, { $set: { classic: true } })
Given the following sample documents in the loans collection:
{ id: 122, book: "ABC", name: "L.A.", date: ISODate("2022-05-20") },
{ id: 343, book: "EFF", name: "T.B.", date: ISODate("2022-05-22") },
{ _id: 454, book: "CFH", name: "M.C.", date: ISODate("2022-05-12") }
What command deletes the document where the value of book is "EFF" and user is "T.B.", and returns the deleted document?
Given the following sample documents in the inventory collection:
{ id: 6305, name : "A. S.", "assignment" : 5, "status" : "A" },
{ id: 6308, name : "B. M.", "assignment" : 3, "status" : "B" },
{ id: 6312, name : "E. M.", "assignment" : 5, "status" : "C" },
{ id: 6319, name : "R. S.", "assignment" : 2, "status" : "D" },
{ id: 6322, name : "A. S.", "assignment" : 2, "status" : "A" },
{ id: 6234, name : "R. S.", "assignment" : 1, "status" : "B" },
{ id: 6235, name : "A. S.", "assignment" : 1, "status" : "C" },
{ id: 6315, name : "E. M.", "assignment" : 3, "status" : "A" }
What expression will remove all documents with "status" : "C" in the inventory collection?
db.inventory.delete ({"status" : "C"})
db.inventory.deleteOne ({"status" : "C"})
db.inventory.deleteMany ({"status" : "C"})
db.inventory.findOneAndDelete ({"status" : "C"})
Given the following documents in the ratings collection:
{ id: 0, hotel: "AAA", rating: 4.5 },
{ id: 1, hotel: "BBB", rating: 3.0 },
{ _id: 2, hotel: "CCC", rating: 4.2 }
What mongosh command will return a document for hotel "CCC"?
db.ratings.return_one( {hotel: "CCC"} )
db.ratings.find_one( {hotel: "CCC"} )
db.ratings.returnOne( {hotel: "CCC"} )
db.ratings.findOne( {hotel: "CCC"} )
The following query generates a collection scan:
db.people.find({employer : "ABC" }).sort ({last_name:1 , job:1})
Which two indexes will most improve the performance of the query?
Choose 2
db.people.createIndex({employer:1, last_name : 1 , job : 1 } )
db.people.createIndex({employer:1, last_name : -1 , job : -1 } )
db.people.createIndex({employer:1,last_name : -1 , job : 1 } )
db.people.createIndex({employer:1,last_name : 1 , job : -1 } )
Given a collection called collection, in which all documents have the following shape:
{ _id:1, objs:[ {a:1,b:2},{a:2,b:1} ] }
And the query on this collection:
db.collection.find({"objs.a":1})
What index will support this query?
{"objs.a":1}
{objs:1}
{objs:1,"objs.a"1}
{objs:1,"a"1}
Given the following query:
db.coll.find({}).sort({"product": 1, "price": 1})
Which two indexes will improve the performance of this query?
(Choose 2)
{"product": 1, "price": 1}
{"product": 1, "price": -1}
{"product": -1, "price": 1}
{"product": -1, "price": -1}
