How to perform keyword query in MongoDB

mongodb, nosql

Solution

The documentation has a chapter about Full Text Search in Mongo which recommends to split each string into an array of individual words, so that you can index it for faster lookup.

{   Id:001,
    Name:"C# step by step",
    Description:"Programming book on C#",
    Description_tags: [
        "Programming",
        "book",
        "on",
        "C#"]
}

db.books.ensureIndex({Description_tags:1});

db.books.find(Description_tags:"Programming");

When speed is not an issue and you want to avoid changing your data structure, you can also use regular expressions. But keep in mind that this scales badly: the lookup time will be linear to the number of entries in the collection.

db.collection.find( {description: { $regex: "Programming"} } );

Problem

Here I have a BOOK collection: ``` Book Id:001 Book Name:C# step by step Book Description:Programming book on C# Book Id:002 Book Name:Head First Java Book Description:Programming Book on Java Book Id:003 Book Name:Steve Jobs Book Description:biography of Jobs ``` My question is how to search all books with the word "Programming" in Book Description? I am newbie to NoSQL, currently I can only use find() to perform precise search rather than a keyword search.

Original source