在 mongodb 中找到一个或另一个词

Finding one word or another in mongodb

我是 MongoDB 的新手,所以我想知道如何在文档中搜索一个词或另一个词,尤其是当一个词是词组而另一个词是单个词时。

这里如果要搜索word printer or ink,我们使用下面的语法。

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

如果我想找字"printer ink"或论文怎么办?请注意 Printer ink 是一个短语。我试过了

db.supplies.runCommand("text", {search:"\"printerink\" paper"}) 

但这并没有帮助,所以我希望有人能帮助我。

引用文档

If the $search string includes a phrase and individual terms, text search will only match the documents that include the phrase. More specifically, the search performs a logical AND of the phrase with the individual terms in the search string

据说要查找单词 printer inkpaper,您将需要 运行 两个不同的查询

db.supplies.find({ $text: { $search: "\"printer ink\"" }})

db.supplies.find({ $text: { $search: "paper" }})

如果任务不需要使用 $text

,您可以使用 $regex 运算符
db.supplies.find({ <yourfield>: {"$in": [/printer ink/, /paper/] }})