0

Using the following code with perfect results, pulling records from Mongo.

@app.route('/search-all/', methods=['GET', 'POST'])
def search_all():
    query = request.form.get("SearchBox")
    try:
        for x in db['mongo-moviedb'].find({"Films.Title" : f"{query}" }, {"Films.$": 1}):
            title = x["Films"][0]["Title"]
            genre = x["Films"][0]["Genre"]
            cast = x["Films"][0]["Actors"]
            plot = x["Films"][0]["Plot"]
        return render_template('Date-Night.html', title=title,genre=genre,cast=cast,plot=plot)
            
    except (RuntimeError, TypeError, NameError):
            error = 'Not found.'
            return render_template('Date-Night.html', error = error)

The problem is it won't pull partial matches. I can get exact matches or the error prompt. I've tried using $regex and /like/ in the find() function to no avail. Haven't been able to find anything that returns a result that only partially matches the record. Also it has to include the 'query' variable pulled from form input.

Ex: 'Star' would return Star Wars, Starman, Wish Upon a Star....etc.

Thanks.

Example:

enter image description hereenter image description here

davidism
  • 121,510
  • 29
  • 395
  • 339
  • What does the `query` variable actually contain? – Joe Dec 27 '22 at 01:27
  • "_I've tried using $regex_" - can you create and share a mongoplayground.net example that demonstrates the problem? – user20042973 Dec 27 '22 at 01:35
  • @joe The variable 'query' contains the contents of a search box. – Michael Jason Stokes Dec 27 '22 at 01:36
  • so it is a user-provided json structure? – Joe Dec 27 '22 at 01:37
  • 2
    If it is a string, did you try implementing regex like https://stackoverflow.com/a/50485848/2282634 (without the `^` if you want it unanchored). Note that it won't be able to use any indexes. – Joe Dec 27 '22 at 01:41
  • Added photos for clarity. If the Search query (variable 'query') isn't an exact match, nothing is returned. I'd like it to at least match with results that have whole words in common with the query. – Michael Jason Stokes Dec 27 '22 at 01:41
  • you might also consider a text index https://stackoverflow.com/questions/17792267/mongodb-text-index-search-slow-for-common-words-in-large-table – Joe Dec 27 '22 at 01:43
  • Yeah, I tried the regex syntax I'm familiar with $regex and the /like/ parameters in the find() function. It only returned the error. I mat not have it in the right place but based on documentation it seems maybe this isn't the right way to query the JSON file. – Michael Jason Stokes Dec 27 '22 at 01:43
  • As far as I can tell, you have not shared the query you are running, the error you are receiving, or an example reproducible example/playground. Using `$regex` with `"Spec"` from your screenshots seems to work fine [here](https://mongoplayground.net/p/f_7zB4pSXWt). An alternative approach would be to use Atlas Search, there's a tutorial about building a movie searching application using it [here](https://www.mongodb.com/developer/products/atlas/build-movie-search-application/) – user20042973 Dec 27 '22 at 04:28
  • Yes, it works really well that way (on the playground) but how would you insert a variable instead of a string? I'm using f-strings so I'm doing like f"{variable}" which just doesn't seem to work in this case. – Michael Jason Stokes Dec 27 '22 at 21:56

1 Answers1

0

The correct syntax was:

for x in db['mongo-moviedb'].find({"Films.Title" : {"$regex" :f"{query}"} }, {"Films.$": 1}):

Thanks, Joe! It was in the first link you sent.