mongodbkeyword-searchmatch-phrasedatabase

How do I search for a phrase and a keyword at the same time in mongodb?


Basically I'm going through a large yelp database in my mongodb, and I want to find all reviews with the phrase hot wings and keyword discount. But I can't create the right query that will look for the review that has the phrase hot wings AND the keyword discount in the review.

To give you some background, I imported several large json files of yelp review into my mongodb database on ubuntu. I want to look for keyword "UFC" under "text" and "Alcohol: full_bar" under attributes and return a count of them, at bare minimum. Because I want to see if bars that mentioned UFC and MMA get more reviews and checkins and tips than other bars, that do not mention those things.

I've built this index successfully in my mongodb database:

> db.collection.createIndex({"text":"text", "attributes": "text"})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}

This is what I have:

db.collection.find( { $text: { $search: "\"hot wings\" 'discount'" } } )

So far it only gave me reviews with hot wings in there, and discount in there. Is there way to show only reviews contains phrase hot wing AND keyword discount?

And is there way to search for multiple phrases and a key word at the same time?


Solution

  • this should work:

    db.collection.find({
    $and: [
        {'text': {'$regex': 'hot wings'} },
        {'text': {'$regex': 'discount'} }
    ]})
    

    You can add more phrases to the $and array.