C100DBA practice question 6 of 25

Which of the following is incorrect statement about find and findOne operations in MongoDB?

Full set: 130 questions
Answer options
A: findQ returns all the documents in a collection while findOne() retrieves only the first one.
B: find.limit(l) is not the same query as findOne()
C: findOneQ returns the actual first document retrieved from a collection
D: findQ and findOneQ returns cursors to the collection documents Correct answer
Correct Answer:
D. findQ and findOneQ returns cursors to the collection documents
Explanation

The statement that both find() and findOne() return cursors is incorrect. The db.collection.find() method returns a cursor object, which points to the result set of a query. The client application can then iterate through the cursor to retrieve the documents. In contrast, the db.collection.findOne() method executes a search for a single document and returns that one document object directly, not a cursor. This makes findOne() a more direct way to retrieve a single record.

Why Incorrect

A. This statement is functionally correct; find() is used to get multiple documents (via a cursor) and findOne() gets the first one.
B. This is a correct statement. find().limit(1) returns a cursor, whereas findOne() returns a document object, making their return types different.
C. This is a correct statement. findOne() retrieves and returns the first document that matches the query criteria based on the natural order or a specified sort order.

References

1. MongoDB Manual, "db.collection.findOne()". The documentation explicitly states, "Returns one document that satisfies the specified query criteria on the collection or view." The return value is described as a document.

2. MongoDB Manual, "db.collection.find()". The documentation states, "Selects documents in a collection or view and returns a cursor to the selected documents."

3. MongoDB University, M001: MongoDB Basics, "findOne vs find". The course material distinguishes between the two methods, highlighting that find() returns a cursor that can be iterated, while findOne() returns the document itself.

Scroll to Top