nerdexam
MongoDB

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

Performance Tuning

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)
  • A
    3% (2)
  • B
    8% (5)
  • C
    88% (56)
  • D
    2% (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; createIndex builds a new index on a specified field, not rebuilds existing ones, and reIndex is not a valid field usage here.
  • B - reIndex({author:1}) is wrong because reIndex() accepts no arguments; passing a field specification is invalid syntax.
  • D - createIndex({author:1}).reIndex() is wrong because createIndex returns 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

#reIndex#index rebuild#collection maintenance

Community Discussion

No community discussion yet for this question.

Full C100DBA Practice