admin管理员组文章数量:1302406
I want to create advanced search in project that is in Node JS with Mongo DB. I have created a field in DB that is search-key. user can type any word in text box and i want to match in this word in string and fetch the result. below my db structure and code. my code work only match first word of string not search in all string.
DB :
{
"_id": ObjectId("001"),
"searchkey": "Test Product Test Product T-shirt Half sleeves Adidas"
}
{
"_id": ObjectId("123"),
"searchkey": "boy test Product Test Product T-shirt Half sleeves Nike"
}
{
"_id": ObjectId("456"),
"searchkey": "girl test Product Summer Product T-shirt full sleeves Adidas"
}
{
"_id": ObjectId("789"),
"searchkey": "any product any Product any Product T-shirt full sleeves Adidas"
}
{
"_id": ObjectId("1010"),
"searchkey": "woodland Product woodland Product T-shirt Half sleeves woodland"
}
{
"_id": ObjectId("1212"),
"searchkey": "Summer Product Test Product T-shirt Half sleeves Adidas"
}
My Query :
Collection.find({searchkey : {$regex: new RegExp('^' + search.toLowerCase(), 'i')},searchkey : {$regex: new RegExp('^' + search.toUpperCase(), 'i')},is_active:true},function(error,fetchSearch){
console.log(fetchSearch);
});
If i search test or TEST
it will give me only one result that id is 001 and rest of the result not match from all string.
I want like if i search summer
or Summer
that it will give me 456 and 1212
_Id data.
I want to create advanced search in project that is in Node JS with Mongo DB. I have created a field in DB that is search-key. user can type any word in text box and i want to match in this word in string and fetch the result. below my db structure and code. my code work only match first word of string not search in all string.
DB :
{
"_id": ObjectId("001"),
"searchkey": "Test Product Test Product T-shirt Half sleeves Adidas"
}
{
"_id": ObjectId("123"),
"searchkey": "boy test Product Test Product T-shirt Half sleeves Nike"
}
{
"_id": ObjectId("456"),
"searchkey": "girl test Product Summer Product T-shirt full sleeves Adidas"
}
{
"_id": ObjectId("789"),
"searchkey": "any product any Product any Product T-shirt full sleeves Adidas"
}
{
"_id": ObjectId("1010"),
"searchkey": "woodland Product woodland Product T-shirt Half sleeves woodland"
}
{
"_id": ObjectId("1212"),
"searchkey": "Summer Product Test Product T-shirt Half sleeves Adidas"
}
My Query :
Collection.find({searchkey : {$regex: new RegExp('^' + search.toLowerCase(), 'i')},searchkey : {$regex: new RegExp('^' + search.toUpperCase(), 'i')},is_active:true},function(error,fetchSearch){
console.log(fetchSearch);
});
If i search test or TEST
it will give me only one result that id is 001 and rest of the result not match from all string.
I want like if i search summer
or Summer
that it will give me 456 and 1212
_Id data.
3 Answers
Reset to default 5You are only checking if the string is starting with the user's search string.
^
= means start of string, so '^' + search.toLowerCase()
means search for a string which is starting with search.toLowerCase()
, and that's it. If it wont start with the search string - it wont match. That's why for test
only id 001 get a match.
Change your search pattern to: '.*' + search.toLowerCase() + '.*'
to find the search string anywhere inside the strings.
Here is the new regex, you can see it is matching 'Summer' with both 456 and 1212
Change your query like this:
db.getCollection('Stackoverflow').find({ searchkey: {$regex: "test", $options: "$i"}})
Already answered https://stackoverflow./a/50693553/6517383. Here is a simple function I made to search using subwords in node. Hope it helps someone
Let's suppose a user search for pri nks
so it should satisfy printer and inks but $text search doesn't allow for this so here is my simple function:
var makeTextFilter = (text) => {
var wordSplited = text.split(/\s+/);
/** Regex generation for words */
var regToMatch = new RegExp(wordSplited.join("|"), 'gi');
let filter = [];
searchFieldArray.map((item,i) => {
filter.push({});
filter[i][item] = {
$regex: regToMatch,
$options: 'i'
}
})
return filter;
}
and use it in your query like this
let query = {...query, $or: makeTextFilter(textInputFromUser)}
tableName.find(query, function (err, cargo_list)
本文标签: javascriptSearch any word in string mongodb with node jsStack Overflow
版权声明:本文标题:javascript - Search any word in string mongodb with node js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741681386a2392191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论