MongoDB Mock Test 1 Flashcards
- Which numeric type is a valid MongoDB BSON type?
(Choose 1)
A. Float
B. Number
C. BIGINT
D. 32-bit integer
(D) 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' }
Which two documents can successfully be added in the same collection?
(Choose 2)
A. { _id: 1, n: [1,2,5], p: 0.75, c: 'Green' } B. { _id: 5, n: [1,2,5], p: 0.75, c: 'Green' } C. { _id: 2, n: [1,2,5], p: 0.75, c: 'Green' } D. { _id: 6, n: [1,3,7], p: 0.85, c: 'Orange }
(B)
~~~
{ _id: 5, n: [1,2,5], p: 0.75, c: ‘Green’ }
~~~
(D)
~~~
{ _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"}
Which two documents can successfully be added in the same collection?
(Choose 2)
A. {_id: 0, txt: "just some text"} B. {_id: 1, txt: "just some text"} C. {_id: [4], txt: "just some text"} D. {_id: 3, txt: "just some text"}
(A)
~~~
{_id: 0, txt: “just some text”}
~~~
(D)
~~~
{_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?
(Choose 1)
A. db.files.insertOne({ file: "a.log", owner: "applicationA", size: 1KB, deleted: true }) B. db.files.insertOne({ file: "a.log", owner: "applicationA", size: 1KB, deleted: True }) C. db.files.insertOne({ file: "a.log", owner: "applicationA", size: 1024, deleted: true }) D. db.files.insertOne({ file: "a.log", owner: "applicationA", size: 1024, deleted: True })
(C)
~~~
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}]}]})
What is the correct output of the query?
Choose 1)
A. { "name" : "XPhone", "price" : 799, "color" : [ "white", "black" ], "storage" : [ 64, 128, 256 ] } B. { "name" : "XPad", "price" : 899, "color" : [ "white", "black", "purple" ], "storage" : [ 128, 256, 512 ] } C. { "name" : "GPhone", "price" : 599, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256, 512 ] } D. { "name" : "GPad", "price" : 699, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256, 1024 ] }
(D)
{ "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?
(Choose 1)
A. db.inventory.findOne()
B. db.inventory.find().toArray();
C. db.inventory.find();
D. db.inventory.findMany().toArray()
(B)
db.inventory.find().toArray();
A collection has documents like the following:
{ _id: 1, name: 'Oatmeal Fruit Cake with Gummy Bears ', price: 11)}, { _id: 2, name: 'Cheesecake Trifle with Chocolate Sprinkles ', price: 14)}, { _id: 3, name: 'Pistachio Brownie with Walnuts ', price: 5}, { _id: 4, name: 'Strawberry Ice Cream Cake with Butterscotch Syrup ', price: 3)}
How should the ‘autocomplete’ index be defined to look for matches at the beginning of a word on the name field?
(Choose 1)
A. { "mappings": { "dynamic": false, "fields": { "name": [ { "type": "autocomplete", "tokenization": "regexCaptureGroup"} ] } }} B. { "mappings": { "dynamic": false, "fields": { "name": [ { "type": "autocomplete", "tokenization": "edgeGram"} ] } }} C. { "mappings": { "dynamic": false, "fields": { "name": [ { "type": "autocomplete", "tokenization": "nGram"} ] } }} D. { "mappings": { "dynamic": false, "fields": { "name": [ { "type": "autocomplete", "tokenization": "matchNGram"}} ] } }}
(B)
{ "mappings": { "dynamic": false, "fields": { "name": [ { "type": "autocomplete", "tokenization": "edgeGram"} ] } }}
Given the following sample documents:
{_id:1, name: "Quesedillas Inc.", active: true }, {_id:2, name: "Pasta Inc.", active: true }, {_id:3, name: "Tacos Inc.", active: false }, {_id:4, name: "Cubanos Inc.", active: false }, {_id:5, name: "Chicken Parm Inc.", active: false }
A company wants to create a mobile app for users to find restaurants by name. The developer wants to show the user restaurants that match their search. An Atlas Search index has already been created to support this query.
What query satisfies these requirements?
(Choose 1)
~~~
A. db.restaurants.aggregate([{ “$search”: { “text”: { “path”: “name”, “synonym”: “cuban”} } }])
B. db.restaurants.aggregate([{ “$search”: { “text”: { “path”: “name”, “query”: “cuban”} } }])
C. db.restaurants.aggregate([{ “$search”: { “text”: { “field”: “name”, “query”: “cuban”} } }])
D. db.restaurants.aggregate([{ “$search”: { “text”: { “field”: “name”, “synonym”: “cuban”} } }])
~~~
(B)
db.restaurants.aggregate([{ "$search": { "text": { "path": "name", "query": "cuban"} } }])
Given the data set and query:
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "player" : "p1", "score" : 89 } { "_id" : ObjectId("512bc962e835e68f199c8687"), "player" : "p2", "score" : 85 } { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "player" : "p2", "score" : 65 } { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "player" : "p3", "score" : 65 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "player" : "p3", "score" : 75 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "player" : "p5", "score" : 70 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "player" : "p6", "score" : 100 } db.scores.aggregate( [{ $group: { _id: '$player', score: { $avg: '$score' } }, { $match: { score: { $gt: 70 } } ])
What is the output?
(Choose 1)
A. { "player" : "p1", "score" : 89 } { "player" : "p2", "score" : 85 } { "player" : "p3", "score" : 75 } { "player" : "p6", "score" : 100 } B. { "player" : "p1", "score" : 89 } { "player" : "p2", "score" : 75 } { "player" : "p6", "score" : 100 } C. { "player" : "p1", "score" : 89 } { "player" : "p2", "score" : 75 } { "player" : "p3", "score" : 70 } { "player" : "p5", "score" : 70 } { "player" : "p6", "score" : 100 } D. { "player" : "p1", "score" : 89 } { "player" : "p2", "score" : 75 } { "player" : "p3", "score" : 70 } { "player" : "p6", "score" : 100 }
(B)
{ "player" : "p1", "score" : 89 } { "player" : "p2", "score" : 75 } { "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} After executing the following aggregation pipeline: db.getSiblingDB("mdb").coll.aggregate([ { $out: {db:'test', collection:'results'}} ])
What are two expected results?
(Choose 2)
A. Collection results
is created in database test
.
B. There is a syntax error command. Collection results
is not created.
C. No documents in collection coll
are written to collection results
.
D. All documents in collection coll
are written to collection results
.
(B) There is a syntax error command. Collection results
is not created.
(C) No documents in collection coll
are written to collection results
.
The syntax error is the use of $out in the aggregation stage. It should be used outside of the aggregation pipeline.
Given the following documents:
{_id:1, a: "one", b: "four"} {_id:2, a: "two", b: "four"} {_id:3, a: "three", b: "four", c: "three"} If the following command is executed: db.coll.replaceOne({}, {a: "ten", b: "five"})
What is the result?
(Choose 1)
A. {_id:1, a: "ten", b: "five"} {_id:2, a: "ten", b: "five"} {_id:3, a: "ten", b: "five"} B. {_id:1, a: "ten", b: "five"} {_id:2, a: "two", b: "four"} {_id:3, a: "three", b: "four", c: "three"} C. {_id:1, a: "ten", b: "five"} {_id:2, a: "ten", b: "five"} {_id:3, a: "ten", b: "five", c: "three"} D. {_id:1, a: "one", b: "four"} {_id:2, a: "two", b: "four"} {_id:3, a: "three", b: "four", c: "three"}
(B)
```
{_id:1, a: “ten”, b: “five”} {_id:2, a: “two”, b: “four”} {_id:3, a: “three”, b: “four”, c: “three”}
~~~
The replaceOne
method updates the first document that matches the filter with the replacement document. In this case, the filter is empty, meaning it will match the first document in the collection.
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()?
(Choose 1)
A. [{_id:1, b:2}, {_id:2, b:2}] B. [{_id:1, a:1, b:2}, {_id:2, a:2}] C. [{_id:1, a:1, b:1}, {_id:2, a:2, b:2}] D. [{_id:1, a:1, b:2}, {_id:2, a:2, b:2}]
(D)
[{_id:1, a:1, b:2}, {_id:2, a:2, b:2}]
Given the following documents:
{_id:1, a: “one”, b: “four”}
{_id:2, a: “two”, b: “four”}
{_id:3, a: “three”, b: “four”, c: “three”}
If the following command is executed:
db.coll.replaceOne({}, {a: “ten”, b: “five”})
~~~
What is the result?
(Choose 1)
A. {_id:1, a: "ten", b: "five"} {_id:2, a: "ten", b: "five"} {_id:3, a: "ten", b: "five"} B. {_id:1, a: "ten", b: "five"} {_id:2, a: "two", b: "four"} {_id:3, a: "three", b: "four", c: "three"} C. {_id:1, a: "ten", b: "five"} {_id:2, a: "ten", b: "five"} {_id:3, a: "ten", b: "five", c: "three"} D. {_id:1, a: "one", b: "four"} {_id:2, a: "two", b: "four"} {_id:3, a: "three", b: "four", c: "three"}
(B)
{_id:1, a: "ten", b: "five"} {_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()?
(Choose 1)
A. [{_id:1, b:2}, {_id:2, b:2}] B. [{_id:1, a:1, b:2}, {_id:2, a:2}] C. [{_id:1, a:1, b:1}, {_id:2, a:2, b:2}] D. [{_id:1, a:1, b:2}, {_id:2, a:2, b:2}]
(D)
[{_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?
(Choose 1)
A. db.cakeFlavors.updateOne({ flavor: "strawberry"} , { $set: { number: 100 } }, { $upsert: true }) B. db.cakeFlavors.insertOne({ flavor: "strawberry"} , { $set: { number: 100 } }, { $upsert: true }) C. db.cakeFlavors.insertOne({ flavor: "strawberry"} , { $set: { number: 100 } }, { upsert: true }) D. db.cakeFlavors.updateOne({ flavor: "strawberry"} , { $set: { number: 100 } }, { upsert: true })
(D)
db.cakeFlavors.updateOne({ flavor: "strawberry"} , { $set: { number: 100 } }, { upsert: true })