查询字段值,该字段值是搜索值的子字符串

时间:2019-03-23 12:05:39

标签: mongodb mongoose

如果我有以下文件,

`
db.articles.insert(
   [
     { _id: 1, subject: "coffee", author: "xyz", views: 50 },
     { _id: 2, subject: "Coffee Shopping", author: "efg", views: 5 },
     { _id: 3, subject: "Baking a cake", author: "abc", views: 90  },
     { _id: 4, subject: "baking", author: "xyz", views: 100 },
     { _id: 5, subject: "Café Con Leche", author: "abc", views: 200 },
     { _id: 6, subject: "Сырники", author: "jkl", views: 80 },
     { _id: 7, subject: "coffee and cream", author: "efg", views: 10 },
     { _id: 8, subject: "Cafe con Leche", author: "xyz", views: 10 }
   ]
)
`

一个人如何显示以下结果

`
{ _id: 3, subject: "Baking a cake", author: "abc", views: 90  }
`

有人搜索术语cakemaster吗?

我尝试了以下方法,

`
const regex = new RegExp(escapeRegex(req.query.search), 'gi');
db.articles.createIndex( { subject: "text" } )
db.articles.find({$text:{$search: "cakes"}})
`

但这不起作用。

0 个答案:

没有答案