7

im using nodejs and mongoose to build an api, im trying to do the search feature, but it deosnt seem to query anything, the code.

app.get('/search', function(req,res){
    return Questions.find({text: "noodles"}, function(err,q){
        return res.send(q);

    });
});

its not giving me any results, i know thier should be at least 4 results from this query, thiers 4 documents in the questions database with the word "noodles", everything is working the the database connection and my node server

1 Answer 1

15

What your query is doing is finding documents where the text property matches "noodles" exactly. Assuming you're trying to query for documents where the text property simply contains "noodles" somewhere, you should use a regular expression instead:

app.get('/search', function(req,res){
    var regex = new RegExp('noodles', 'i');  // 'i' makes it case insensitive
    return Questions.find({text: regex}, function(err,q){
        return res.send(q);
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

how can i send regex as a parameter for the api, for example /search/regex
thanks i solved it i just have to send regex as a param like so /search/:key, and then in the body just have var regex = new RegExp(req.params.key,'i'); :)
You could also use queries like so. /search/?q=key and then var regex = new RegExp(req.query.q);
thx for the solution. though it seems that 'i' doesn't make it case sensitive
@mdehghani Hmm...the docs for RegExp showing the i option are here. Maybe post a new question if it's still not working for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.