Searching for keywords in a document is a common operation for many applications. If this is a core operation, it makes sense to use a specialized store for search, such as Elasticsearch; however, MongoDB can be used efficiently until scale dictates moving to a different solution.
The basic need for a keyword search is to be able to search the entire document for keywords. For example, with a document in the products collection, as shown in the following code:
{ name : "Macbook Pro late 2016 15in" ,
manufacturer : "Apple" ,
price: 2000 ,
keywords : [ "Macbook Pro late 2016 15in", "2000", "Apple", "macbook", "laptop", "computer" ]
}
We can create a multi-key index in the keywords field, as shown in the following code:
> db.products.createIndex( { keywords: 1...