1

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]}
   }
)

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have an Event model with fields like approved, banned, reviewed and so on. These are ALWAYS queried. But also search fields like country region city. These get requested depending on the users behaviour. Last but not least the user is able to search on either or both category_ids and musicstyle_ids of that Events, which are Arrays.
I did some google and found this stackoverflow.com/questions/25059525/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.