I have a large MongoDb collection, where users can search on multiple Array fields. How is the best way to index that? I saw that creating indexes for multiple Array indexes ends up with:
"Mongo::Error::OperationFailure: cannot index parallel arrays"
Example query:
db.collection.find(
{category_ids: {$in: [object_id, object_id]},
second_category_ids: $in: {[object_id, object_id]}
}
)
As per mongoDB documentation, multikey index on parallel arrays is not allowed. Below is what documentation reads-
As such, you cannot create a compound multikey index if more than one to-be-indexed field of a document is an array. Or, if a compound multikey index already exists, you cannot insert a document that would violate this restriction.
At max we can do is index both fields separately and query will use the best index it feels. If you have better idea than query planner then hint can also be used to use specific index.
Or you can think for restructuring your schema. For example subcategories can be taken in different collection. May be I can help if I get to know more about data pattern and schema design.