简体   繁体   中英

Not getting proper output from regex in mongodb.find() function

I have a function called isNearestDataExist. I want it to return query result like mysql 'LIKE' operator. Where it can send response if only one word is there in the sentence. Example input: 'what is your name?', 'name?', 'your name' etc output: 'Some name'

SELECT answer FROM tbl_chats WHERE asked_column IS LIKE '%input%'; This query is returning proper response from mysql. But in mongoDB its only giving response when the whole exact sentence is given as an input. I tried with many techniques but i couldn't get the proper response like mysql. Kindly help me. Sorry for my poor English. Thanks in advance.

def isNearestDataExist(self, col_name, key, value):
    col_name = 'col_chats'
    key = 'asked'
    value = 'what is your name?'
    result = Mongo.db[col_name].find( {key: {"$regex": '/'+value+'/'}})
    return result

try this.

def isNearestDataExist(self, col_name, key, value):
    col_name = 'col_chats'
    key = 'asked'
    #value = 'what is your name?'
    regx = re.compile(value, re.IGNORECASE)
    result = Mongo.db[col_name].find( {key: regx})
    return result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM