It is done for more flexibility, but usually we don't need to index on multiple fields.
Let's write helper function which will allow to create such index in 1 line of code (with 2 args - field name and index value, i.e. "owner_id" and 1), but if need we'll still could do it with an old way.
Analysts are often using Mongo aggregation queries with
$lookup
, like this one:Such queries are very slow. Let's run such query when we have ~590.000 comments. Result: 15:32 - Started 15:38 - Ended
It is so slow because we using
comment_object.author
field which is just string, and not indexed ObjectId field.Let's add
comment_object.author_id
field with index on it, and run slightly rewrited query after full re-sync:Result: 15:43:02 - Started 15:43:03 - Ended
Indexes could strongly improve performance of using & development of Mongo aggregation queries.
In this issue:
It is done for more flexibility, but usually we don't need to index on multiple fields. Let's write helper function which will allow to create such index in 1 line of code (with 2 args - field name and index value, i.e. "owner_id" and 1), but if need we'll still could do it with an old way.