C100DBA · Question #100
Consider a collection posts which has fields: Jd, post_text, post_author, post_timestamp, post_tags etc. Which of the following query retrieves ONLY the key named post_text from the first document…
The correct answer is A. db.posts.finOne({},{_id:0, post_text:1}). Option A is correct because db.posts.findOne({}, {_id:0, post_text:1}) uses two arguments properly: an empty filter {} (match any document) and a projection {_id:0, post_text:1} that explicitly includes only post_text and suppresses the default _id field - returning exactly one…
Question
Consider a collection posts which has fields: Jd, post_text, post_author, post_timestamp, post_tags etc. Which of the following query retrieves ONLY the key named post_text from the first document retrieved?
Options
- Adb.posts.finOne({},{_id:0, post_text:1})
- Bdb.posts.findOne({post_text: 1})
- Ddb.posts.finOne«},{post_text:l})
How the community answered
(49 responses)- A90% (44)
- B4% (2)
- D6% (3)
Explanation
Option A is correct because db.posts.findOne({}, {_id:0, post_text:1}) uses two arguments properly: an empty filter {} (match any document) and a projection {_id:0, post_text:1} that explicitly includes only post_text and suppresses the default _id field - returning exactly one field from the first document found.
Option B is wrong because {post_text: 1} is passed as the filter, not a projection - this tells MongoDB to find a document where post_text equals 1, not to project the field; there is no projection argument at all.
Option D is wrong for two reasons: finOne is a typo (the method is findOne), and the projection {post_text:l} uses the letter l instead of the number 1, making it syntactically invalid.
Memory tip: Think of findOne(filter, projection) as two separate jobs - the first {} answers "which document?" and the second {} answers "which fields?" - and remember you must explicitly set _id:0 to hide it, since MongoDB includes _id by default.
Topics
Community Discussion
No community discussion yet for this question.