C100DBA · Question #2
Which of the following commands finds all the documents in the posts collection with post timestamp field as null?
The correct answer is C. db.posts.find( { post_timestamp : { $type; 10 > > ). Option C is correct because MongoDB uses the $type operator to query by BSON data type, and the BSON type code for null is 10 - so the valid query is db.posts.find({ post_timestamp: { $type: 10 } }). The garbled syntax in option C is a formatting artifact; the underlying concept
Question
Which of the following commands finds all the documents in the posts collection with post timestamp field as null?
Options
- Adb.posts.find( { post_timestamp: { $fieldtype: null } } )
- Bdb.posts.find( { post_timestamp: { $type: null } } )
- Cdb.posts.find( { post_timestamp : { $type; 10 > > )
- Ddb.posts.find( { post_timestamp: { $fieldtype: 10 } } )
How the community answered
(13 responses)- C92% (12)
- D8% (1)
Explanation
Option C is correct because MongoDB uses the $type operator to query by BSON data type, and the BSON type code for null is 10 - so the valid query is db.posts.find({ post_timestamp: { $type: 10 } }). The garbled syntax in option C is a formatting artifact; the underlying concept it represents ($type with value 10) is the only valid approach shown.
Why the distractors fail:
- A & D use
$fieldtype, which is not a real MongoDB operator - it simply doesn't exist. - B passes
nullas the argument to$type($type: null), but$typeexpects a BSON type number (or string alias like"null"), not the null value itself.
Memory tip: Think "type TEN for null" - the BSON type table lists null at position 10, so $type: 10 is your null-detector. You can also use the string alias $type: "null" as an alternative, but the numeric code 10 is what exam questions most commonly test.
Topics
Community Discussion
No community discussion yet for this question.