节点JS
search_key = new RegExp('.*' + req.params.search_key + '.*', 'i');
Item.find({product_name:search_key}).exec(function (err, items) {
console.log(items) );
});
在这里,我可以使用search_key搜索产品名称。但是我的问题是,我的产品名称是“ COMPLAN MILK BISCUITS 100GM
”。如果我搜索“ BISCUITS COMPLAN
”,则找不到该产品。我需要根据搜索中的包含条件找到“ BISCUITS COMPLAN
”和“ COMPLAN BISCUITS
”。
您需要创建文本索引来实现您的目标,然后从文本索引字段中进行搜索。mongo shell命令为字段创建文本索引product_name
db.colectionName.createIndex( { product_name: "text" } );
然后您可以使用$text
和搜索$search
。doc
db.collectionName.find({ $text: { $search: "BISCUITS COMPLAN" } });
不需要使用new RegExp(
就可以使用,例如:
Item.find({ $text: { $search: req.params.search_key } }).exec(function (err, items) {
console.log(items) );
});;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句