How to match exact phrase with dynamic string in text index Mongodb?

后端 未结 1 1038
暖寄归人
暖寄归人 2021-01-28 10:08

I have this query

db.words.find({ \"$text\": { \"$search\": \"\\\"cake sale\\\"\" } }) // gives expected answer in robo3T

Now my text

相关标签:
1条回答
  • 2021-01-28 10:59

    Your query is incorrect. You need to change the query

    db.words.find({ "$text": { "$search": `"\"${text}\"` } })
    

    to

    db.words.find({ "$text": { "$search": `\"${text}\"` } })
    

    Since, there is an extra double quote (") in the beginning after the first backquote. Doing that will fix your query.

    Simple illustration:

    console.log("\"cake sale\"");
    
    var text = "cake sale";
    console.log(`\"${text}\"`);
    // both the console.log gives same result

    0 讨论(0)
提交回复
热议问题