C100DBA · Question #79
Which command can be used to rebuild the indexes on a collection in MongoDB?
The correct answer is C. db.collection.relndexQ. db.collection.reIndex() (option C) is the correct MongoDB command for rebuilding all indexes on a collection. It takes no arguments because it operates on all indexes in the collection automatically, dropping and recreating them in place. Why the distractors are wrong: A - create
Question
Which command can be used to rebuild the indexes on a collection in MongoDB?
Options
- Adb.collection.createlndex({relndex:l})
- Bdb.collection.reIndex({author:l})
- Cdb.collection.relndexQ
- Ddb.collection.createIndex({author:l}).reIndex()
How the community answered
(64 responses)- A3% (2)
- B8% (5)
- C88% (56)
- D2% (1)
Explanation
db.collection.reIndex() (option C) is the correct MongoDB command for rebuilding all indexes on a collection. It takes no arguments because it operates on all indexes in the collection automatically, dropping and recreating them in place.
Why the distractors are wrong:
- A -
createIndex({reIndex:1})doesn't exist;createIndexbuilds a new index on a specified field, not rebuilds existing ones, andreIndexis not a valid field usage here. - B -
reIndex({author:1})is wrong becausereIndex()accepts no arguments; passing a field specification is invalid syntax. - D -
createIndex({author:1}).reIndex()is wrong becausecreateIndexreturns a string (the index name), not a collection object, so chaining.reIndex()on it is not valid.
Memory tip: Think of reIndex() like a "refresh all" button - it needs no input because it already knows what to refresh. If it took arguments, it would be creating something new, not rebuilding what's already there.
Topics
Community Discussion
No community discussion yet for this question.