yoklainterview sim

Database Mongodb Indexes Queries Interview Questions

75 verified Database Mongodb Indexes Queries interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Mongodb Indexes QueriesDifficulty 1
When a new regular collection is created in MongoDB, which index exists on it automatically, with no explicit createIndex() call?
  • aA compound index covering every field in the first inserted document
  • bA unique index on the _id field
  • cA text index on all string-typed fields
  • dNo index exists until one is created explicitly
Explanation:MongoDB creates a unique index on _id when a regular collection is created, guaranteeing every document has a unique identifier and fast lookup by it. Specialized collection types can differ; for example, time series collections don't support unique indexes. There is no automatic compound or text index (a, c), and it isn't true that nothing exists by default (d).
Mongodb Indexes QueriesDifficulty 1
db.products.createIndex({ price: 1 });

What does the value 1 mean in this single-field index definition?
  • aThe index stores keys in ascending order
  • bThe index only accepts positive price values
  • cThe field will be indexed exactly once even after updates
  • dThe index applies to the first document inserted only
Explanation:In an index specification, 1 means ascending order and -1 means descending order for that key. It has nothing to do with allowed values (b), re-indexing behavior (c), or scope limited to one document (d) — a single-field index covers every document's value for that field.
Mongodb Indexes QueriesDifficulty 2
db.orders.createIndex({ customerId: 1, orderDate: -1 });

What kind of index does this statement create?
  • aTwo separate single-field indexes, one on customerId and one on orderDate
  • bA text index that treats both fields as searchable strings
  • cA compound index keyed on customerId ascending, then orderDate descending
  • dA multikey index, since two fields are listed in the definition
Explanation:Listing more than one field in a single createIndex() call builds one compound index whose keys are ordered by field order and direction — here customerId ascending first, orderDate descending second — not two separate indexes (a). It isn't a text index (b), and multikey indexing is about array-valued fields, not the number of fields listed (d).
Mongodb Indexes QueriesDifficulty 2
db.products.insertOne({ _id: 1, tags: ["sale", "new", "clearance"] });
db.products.createIndex({ tags: 1 });

Since tags holds an array, what kind of index does MongoDB build here?
  • aThe createIndex() call fails, since array fields cannot be indexed directly
  • bA single-field index that stores only the array's first element as the key
  • cA text index, since arrays of strings are always indexed as searchable text
  • dA multikey index, automatically created, with one index entry per array element
Explanation:MongoDB detects that tags holds an array and automatically builds a multikey index, storing a separate index entry for each element of the array so a document can be found by any of its tags. It doesn't reject array fields (a), it doesn't index only the first element (b), and array indexing isn't automatically a text index — that requires an explicit "text" type (c).
Mongodb Indexes QueriesDifficulty 1
What is the main purpose of a text index in MongoDB, e.g. db.articles.createIndex({ content: "text" })?
  • aIt enables full-text search across the field's word content, used with the $text operator
  • bIt forces every value written to that field to be lowercase before storage
  • cIt replaces the need for the _id index on that collection
  • dIt speeds up exact string equality lookups the same way a regular index does
Explanation:A text index tokenizes the field's contents into words (with stemming and stop-word handling) so $text queries can search for terms rather than exact strings — full-text search is exactly its purpose. It doesn't lowercase stored values (b), doesn't replace _id (c), and for plain exact-match lookups a regular ascending/descending index, not a text index, is the right tool (d).
Mongodb Indexes QueriesDifficulty 2
db.places.createIndex({ location: "2dsphere" });
db.places.find({
  location: { $near: { $geometry: { type: "Point", coordinates: [13.4, 52.5] } } }
});

What is the 2dsphere index designed to support?
  • aSorting numeric fields by two decimal places of precision
  • bEfficient geospatial queries on GeoJSON data, treating coordinates on a sphere (Earth)
  • cStoring two separate copies of the document for redundancy
  • dSplitting the collection into two shards automatically based on location
Explanation:A 2dsphere index indexes GeoJSON points/shapes using spherical geometry, enabling queries like $near, $geoWithin, and $geoIntersects to find nearby or contained locations efficiently. It has nothing to do with decimal precision (a), document redundancy (c), or automatic sharding (d) — sharding requires its own separate configuration.

Test yourself against the 2475-question Database bank.

Start interview