C100DBA practice question 23 of 25

Which command can be used to rebuild the indexes on a collection in MongoDB?

Full set: 130 questions
Answer options
A: db.collection.createlndex({relndex:l})
B: db.collection.reIndex({author:l})
C: db.collection.relndexQ Correct answer
D: db.collection.createIndex({author:l}).reIndex()
Correct Answer:
C. db.collection.relndexQ
Explanation

The db.collection.reIndex() command is the designated method for rebuilding all indexes on a collection. This operation drops all existing user-created indexes and then recreates them from scratch. The id index is not affected. This command is useful for reclaiming disk space or improving performance after index fragmentation or significant data churn. The command takes no arguments.

Why Incorrect

A. This syntax attempts to create a new index on a field named relndex. It does not rebuild existing indexes.
B. The reIndex() command does not accept any arguments. Passing an index key specification like {author:1} results in an error.
D. This is invalid syntax. createIndex() does not return an object on which reIndex() can be chained.

References

1. MongoDB Manual, db.collection.reIndex(), Version 7.0. "The db.collection.reIndex() method drops all indexes on a collection and recreates them. This operation may be expensive for collections that have a large amount of data and/or a large number of indexes."

2. MongoDB Manual, reIndex command, Version 7.0. "The reIndex command drops all indexes on a collection and rebuilds them. Syntax: { reIndex: }."

Scroll to Top