【发布时间】:2017-10-02 10:08:55
【问题描述】:
MongoDB 3.4
我在变量中有一个值。 val1 = "小堡"
我需要在集合存储(名称字段上带有文本索引)中搜索文档为
db.stores.insert([
{ _id: 1, name: "Java Hut", description: "Coffee and cakes" },
{ _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
{ _id: 3, name: "Coffee Shop", description: "Just coffee" },
{ _id: 4, name: "Fort Coffee", description: "Discount clothing" },
{ _id: 5, name: "Java Shopping", description: "Indonesian goods" }
])
当我使用变量对集合进行文本搜索时
db.City.find({$text:{$search: val1}})
它返回 _id : 4 文档,因为它包含 Fort。
我需要进行完全匹配但使用变量。
只有当 val1 = "Fort Coffee" 时,上述搜索才会返回
【问题讨论】:
-
db.City.find({name:val1}) 使用这个
-
但我需要进行文本搜索。
-
为时已晚,但这可能对某人有所帮助,您只需要在引号下提供搜索文本。 val1 = "\"Fort Coffee\""
标签: mongodb text-search exact-match