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.

Share asked Nov 21, 2016 at 9:17 Pritesh MahajanPritesh Mahajan 5,1749 gold badges43 silver badges65 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You 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