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 Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  swapping data in mongodb saisankalpj 0 708 Aug-30-2022, 12:42 PM
Last Post: saisankalpj
  Find and Replace numbers in String giddyhead 2 1,197 Jul-17-2022, 06:22 PM
Last Post: giddyhead
Exclamation MongoDB cannot connect - pymongo speedev 1 2,056 Aug-21-2021, 01:35 PM
Last Post: ndc85430
Bug Djongo (mongoDb) - Django rest framework Problem with search on JsonFields grzybekrafal 0 2,443 May-11-2021, 09:04 AM
Last Post: grzybekrafal
  Find string between two substrings, in a stream of data xbit 1 2,113 May-09-2021, 03:32 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,365 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Bug ? when dataclass field name == field type Cyril 0 1,526 Oct-22-2020, 03:26 AM
Last Post: Cyril
  Retrieve data from MongoDB dsuk 2 2,263 Aug-24-2020, 09:22 PM
Last Post: micseydel
  insert more data at once in MongoDB Leon79 2 2,289 Jul-05-2020, 01:34 PM
Last Post: Leon79
  Find field name from all possible values kashcode 1 2,074 Apr-22-2020, 06:14 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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