Python Forum
find a string in a field in MongoDB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find a string in a field in MongoDB
#1
Hello coders,

I'm a student and I've almost finished my project for my course. As last thing I added a small search bar in the index.html in order to submit an ingredient, and through search.html I will get all the recipes with that ingredient. This is the little form:

        <form id="search-form" action="{{ url_for('search', mysearch=bar) }}" method="POST">
          <input id="search-box" name="bar" type="text" placeholder="Search by any ingredient!" />
          <input id="search-btn" value="" type="submit" />
        </form>
In the main.py I added the following python code:


@app.route('/search/<mysearch>')
def search(mysearch):
    query={"ingredients": { "$regex": "/*mysearch*/i" }}
    return render_template("search.html", recipes=mongo.db.recipes.find(query))
I cannot really understand well the MongoDB documentation, I tried many ways but when I look for a word I just get:
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

I think there is something wrong in both my code in Python and the small form, since the page search.html is well done.

Could you please help me and tell me what I'm doing wrong? Thank you very much!!!

@app.route('/search/<mysearch>')
def search(mysearch):
    query={"ingredients": { "$regex": mysearch }}
    return render_template("search.html", recipes=mongo.db.recipes.find(query))
I changed it in this way, so now if I got to the address bar and I write http://127.0.0.1:5000/search/chicken I get all the recipes with chicken, but if I use the search bar I get the same error.
Reply
#2
I have almost solved, just I cannot manage to look for the word with case insensitive, if I write parmesan it finds it, if I write ParMeSan it doesn't. Any Idea?

@app.route('/search', methods=["POST"])
def search():
    if request.method == "POST":
        data = request.form.to_dict()
        keyword = str(data['mysearch'])
        query={"ingredients": { "$regex": keyword }}
    return render_template("search.html", recipes=mongo.db.recipes.find(query))
Reply
#3
I would recommend forcing everything to be lowercase. You could also look into casefold.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation MongoDB cannot connect - pymongo speedev 1 521 Aug-21-2021, 01:35 PM
Last Post: ndc85430
  Find string between two substrings, in a stream of data xbit 1 776 May-09-2021, 03:32 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 923 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Bug ? when dataclass field name == field type Cyril 0 593 Oct-22-2020, 03:26 AM
Last Post: Cyril
  Retrieve data from MongoDB dsuk 2 1,027 Aug-24-2020, 09:22 PM
Last Post: micseydel
  insert more data at once in MongoDB Leon79 2 1,056 Jul-05-2020, 01:34 PM
Last Post: Leon79
  Find field name from all possible values kashcode 1 855 Apr-22-2020, 06:14 PM
Last Post: deanhystad
  Help to find a string and read the next lines crlamaral 4 1,293 Mar-19-2020, 09:24 AM
Last Post: Larz60+
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 955 Mar-03-2020, 04:47 AM
Last Post: davidshq
  How to Find & Count String Patterns Between two Markers in a HTML file ahmedwaqas92 3 1,406 Aug-19-2019, 10:12 AM
Last Post: ahmedwaqas92

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020