Querying with indexed fields
Indexing increases the query performance tremendously but causes some overheads in write operations. We will learn in great detail about the various types of indexes in the next chapter. When we index fields, it's important to maintain the right order. For example, for the Book
model, if we will always search by the title, and then maybe by the author and published date, it makes sense to create a compound index.
class Book ... index({title: 1, author: 1, published_date: 1} end
So, if we now try to search for books and the title is present, it will always use the BTreeCursor
indexed search that is faster. However, if the title is not used in searches, the BasicCursor
function is relatively slower.
Note
BTreeCursor
, as the name suggests, uses a
Binary Tree for storing the index. This will change our index searches complexity to O(log2n), where n is the number of values that are indexed. When no index is specified on the field, the search is linear O(n). By default...