C100DBA · Question #46
Given a collection posts as shown below having a document array comments, which of the following command will create an index on the comment author descending?
The correct answer is C. db.posts.createIndex({^commerits.author";-!}). Option C is correct because it uses MongoDB's dot notation ("comments.author") to reference a field inside an embedded array, paired with -1 to specify a descending index - this is the standard syntax for indexing fields in subdocuments/arrays. Option A is wrong for two…
Question
Given a collection posts as shown below having a document array comments, which of the following command will create an index on the comment author descending?
Exhibit
Options
- Adb.posts.createIndex({^comments.$.author":-l});
- Bdb. posts.createIndex({^comments.author" :1});
- Cdb.posts.createIndex({^commerits.author";-!});
How the community answered
(34 responses)- A6% (2)
- B9% (3)
- C85% (29)
Explanation
Option C is correct because it uses MongoDB's dot notation ("comments.author") to reference a field inside an embedded array, paired with -1 to specify a descending index - this is the standard syntax for indexing fields in subdocuments/arrays.
Option A is wrong for two reasons: it incorrectly uses the $ positional operator (comments.$.author), which is only valid in update/query operations, not index creation, and it uses -1 with the wrong operator structure.
Option B is wrong because it specifies 1 instead of -1, which creates an ascending index - the question explicitly asks for descending.
Memory tip: Think of MongoDB index direction like a number line - 1 = ascending (positive direction, low to high), -1 = descending (negative direction, high to low). For nested/array fields, always use dot notation ("array.field") with no $ operators, just like you would in a query projection.
Topics
Community Discussion
No community discussion yet for this question.
