0

I have this collection in mongodb. my collection:

 {
"_id": "5ad2079019551a2108588add",
"brand_name": "MAZOLA LIZA"
"name":"pyd"
 }

{
"_id": "5ad2079019551a2108588adf",
"brand_name": "MAZcara"
"name":"rahul"  
}
{
"_id": "5ad2079019551a2108588agf",
"brand_name": jk LIZA"
"name":"qa" 
}

My desired output:

{
"_id": "5ad2079019551a2108588add",
"brand_name": "MAZOLA LIZA"
"name":"pyd"
 }

{
"_id": "5ad2079019551a2108588adf",
"brand_name": "MAZcara"
"name":"rahul"  
}

I am trying to get the documents which are contains MOZ in "brand_name"

I am using flask_pymongo for this.

ill send this MOZ using flask method. (method/MOZ)

my code is,

@app.route('/framework/<name>', methods=['GET'])
def get_one_framework(name):
    framework = mongo.db.offers
    for q in framework.find({"product": {"$regex":"*."+ name+".*"}}):
        print(q)

its not printing any records, but i have matching records

Pyd
  • 6,017
  • 18
  • 52
  • 109
  • You could use `"$regex"` and search for a regex string instead. – Robert Seaman Apr 15 '18 at 15:59
  • @chridam, this is totally different, I am trying in flask application. – Pyd Apr 15 '18 at 16:37
  • The solution is not different, in fact it is `for q in framework.find({"brand_name": rgx }):` where `rgx` is initialised as `import re rgx = re.compile('^' + re.escape(name))`. Check the other marked dupe answer. – chridam Apr 15 '18 at 16:46
  • Please update your question with what you have tried and the results you are getting. – chridam Apr 15 '18 at 16:55
  • do i need to change any index in mlab – Pyd Apr 15 '18 at 17:08
  • edited my question, please check – Pyd Apr 15 '18 at 17:15
  • @pyd the solution is easy. You have uppercase some thing in your database so you should run a `case insensitive` query. like this: `framework.find({"brand_name": {"$regex":"*."+ name+".*", '$options': 'i'}})` – Spiros I. Economakis Apr 15 '18 at 18:00
  • yes it worked with ` {"$regex":name, '$options': 'i'}`without the dot, is there any difference between with and without dots – Pyd Apr 15 '18 at 18:14
  • it depends on the regex you want to apply. if you need more complex regex and more strict you should try different kind of patterns. for your case probably is enough with the dots. – Spiros I. Economakis Apr 15 '18 at 18:18

0 Answers0