MongoDB Text Search AND multiple search words MongoDB Text Search AND multiple search words mongodb mongodb

MongoDB Text Search AND multiple search words


Give a try to:

db.supplies.runCommand("text", {search:"\"printer\" \"ink\""})

Also, here's a quote from docs:

If the search string includes phrases, the search performs an AND with any other terms in the search string; e.g. search for "\"twinkle twinkle\" little star" searches for "twinkle twinkle" and ("little" or "star").

Hope that helps.


You can wrap each word in double-quotes:

let keywords = ctx.params.query.split(/\s+/).map(kw => `"${kw}"`).join(' ');match.$text = { $search: keywords, $caseSensitive: false };

There is a downside if the user inputs a quoted string this will not work. You'd have to parse out quoted strings first.


As @alecxe pointed out earlier, to do AND search on text index column you need to double quote each search word. Below is a quick one-liner for your requirement.

db.supplies.runCommand("text", {search: "printer ink".split(" ").map(str => "\""+str+"\"").join(' ')})