Are there any complex field types, such as arrays or arrays of objects, that cannot be indexed?

  1. Are there any complex field types, such as arrays or arrays of objects, that cannot be indexed in a MongoDB collection?
  2. If so, what are all field types that cannot be indexed?

Hi @Big_Cat_Public_Safety_Act,

General limits are documented in MongoDB Limits and Thresholds and there also are some specific considerations in the documentation for each index type. Some limits will vary depending on your MongoDB server version, so make sure you are reviewing the correct version documentation (or are on the latest release series of MongoDB).

It depends what sort of index you are creating. For example: Compound Multikey Indexes can only include at most one indexed field whose value is an array.

A more common issue is not whether an index can be created, but whether an index can efficiently support the type of query you would like to make. For example, Regular Expressions for pattern matching in strings cannot use indexes efficiently for case-insensitive matches or partial matches that are not anchored to the beginning of key values. However, there are alternatives to regular expressions that are better suited to advanced text search use cases.

Data that is highly nested can also be difficult to query. I recommend reviewing the Building with Patterns series on schema design patterns as well as A Summary of Schema Design Anti-Patterns for some ideas on common approaches for effective data modeling and indexing.

All field types can be added to indexes, but as noted above the more important aspect is whether an index and schema design approach will be effective in supporting your common queries.

With MongoDB’s flexible schema, an index also isn’t associated with a specific data type like a date or a string: documents in the same collection can use different types for a given field by default. If you want to have more rigid control over the allowed types and value ranges for your data, there is a Schema Validation feature that allows you to create validation rules for a collection.

Regards,
Stennie

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.