我有一个搜索参数,它包含3个字段(住宅,nameOrNumber和街道)的组合,在keyup事件中我需要组合上面提到的3个密钥并从MongoDB中获取值
样本搜索参数:
"Plot 227, Rockingham Street"
示例MongoDB对象
{
"_id" : ObjectId("5460e1660ca8560b00048e47"),
"address" : {
"dwelling" : "The Patterdale",
"nameOrNumber" : "Plot 227",
"street" : "Rockingham Street",
"locality" : "Fitzwilliam",
"town" : "Pontefract",
"county" : "West Yorkshire",
"postcode" : "WF95BZ",
"country" : {
"code" : "UK",
"name" : "United Kingdom"
}
}
}
答案 0 :(得分:1)
您可以查看creating text indexes
,也可以在multiple fields
上创建。然后在查找查询中使用$text
运算符来执行搜索,该搜索将查看使用文本索引编制索引的所有字段。来自文档:
MongoDB提供文本索引以支持对集合文档中字符串内容的文本搜索。
例如,您可以创建一个索引,如:
db.collection.ensureIndex(
{
dwelling: "text",
nameOrNumber: "text",
street: "text"
}
)
然后查询:
db.collection.find( { $text: { $search: "Plot 227 Rockingham Street" } } )
那应该找到你要找的文件。
注意:以上示例未经测试。但是,提供的信息应该指导您正确的方向。