在数据库猫鼬中进行文本搜索

Text Search in the database mongoose

我这样执行文本搜索:

Post.find( { $text: { $search: "hello" } }) 
      .then(products => console.log(products))
      .catch(e => console.error(e));

正在查找具有文本“Hello World”但没有“hello3”的对象。 我希望它输出 Hello World 和 hello3。 我怎样才能在猫鼬中做到这一点? 请提前help.Thanks

我不认为文本索引可以做到这一点。您可能想尝试 $regex 搜索。如下所示

db.collectionname.find({"message": {"$regex" :"Hello.*",$options: 'i'} })

您可以像这样使用正则表达式搜索:

db.collectionname.find({message:new RegExp( 'hello' , 'i')})

您可以使用 .trim() 作为您的查询搜索值:)