MongoDB Questions and Answers Part-9

1. When you query a collection, MongoDB returns a ________ object that contains the results of the query.
a) row
b) cursor
c) colums
d) none of the mentioned

Answer: b
Explanation: The mongo shell then iterates over the cursor to display the results.

2. Point out the correct statement.
a) A database is a set of key-value pairs
b) A MongoDB deployment hosts a number of databases
c) A document holds a set of collections
d) all of the mentioned

Answer: b
Explanation: A database holds a set of collections. A collection holds a set of documents.

3. Which of the following method returns true if the cursor has documents?
a) hasMethod()
b) hasNext()
c) hasDoc()
d) all of the mentioned

Answer: b
Explanation: hasNext() returns true if the cursor returned by the db.collection.find() query can iterate further to return more documents.

4. ____________ method renders the document in a JSON-like format.
a) displayjson
b) print
c) printjson
d) printdoc

Answer: c
Explanation: printjson() operation displays all documents.

5. Point out the wrong statement.
a) Documents have static schema in MongoDB
b) Eventually-consistent reads can be distributed over replicated servers
c) Indexes can include keys from embedded documents and arrays
d) none of the mentioned

Answer: a
Explanation: Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.

6. Which of the following method is called while accessing documents using the array index notation?
a) cur.toArray()
b) cursor.toArray()
c) doc.toArray()
d) all of the mentioned

Answer: b
Explanation: The toArray() method returns an array that contains all the documents from a cursor.

7. The mongo shell and the drivers provide several cursor methods that call on the cursor returned by the _______ method to modify its behavior.
a) cursor()
b) find()
c) findc()
d) none of the mentioned

Answer: b
Explanation: The method iterates completely the cursor, loading all the documents.

8. Which of the following method corresponds to Order by clause in SQL?
a) sort()
b) order()
c) orderby()
d) all of the mentioned

Answer: a
Explanation: The sort() method orders the documents in the result set.

9. The __________ method limits the number of documents in the result set.
a) limit()
b) limitOf()
c) limitBy()
d) none of the mentioned

Answer: a
Explanation: limit() corresponds to the LIMIT statement in SQL.

10. Which of the following line skips the first 5 documents in the bios collection and returns all remaining documents?
a) db.bios.find().limit( 5 )
b) db.bios.find().skip( 1 )
c) db.bios.find().skip( 5 )
d) db.bios.find().sort( 5 )

Answer: c
Explanation: The skip() method controls the starting point of the results set.