【发布时间】:2020-07-15 02:15:30
【问题描述】:
我需要在数组元素中执行文本搜索。有可能吗?
在 Node.js 中使用 Mongoose,我的 userSchema 如下所示:
{
_id: "123456",
name: "Lucas"
items: [
{ title: "Shoes", description: "Very nice shoes"},
{ title: "Pants", description: "Great pants!"}
]
}
我尝试像这样添加索引:
userSchema.index({ "items.title": "text", "items.description": "text" });
但是下面的查询什么也不返回:
User.find({ $text: { $search: "Shoes" }});
【问题讨论】:
-
从
mongoshell 尝试时它工作正常。在“Shoes”上的文本搜索将匹配title和description字段,其中文本值“Shoes”和“... nice shoes”是匹配项。 -
你能返回任何记录吗?例如试试 User.find()
标签: node.js mongodb mongoose mongodb-query