用猫鼬api和nodejs搜索数据库? [英] searching database with mongoose api and nodejs?

查看:54
本文介绍了用猫鼬api和nodejs搜索数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用nodejs和mongoose构建了一个api,我试图做搜索功能,但是它似乎并没有查询任何东西,代码.

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);

    });
});

它没有给我任何结果,我知道该查询至少应有4个结果,问题数据库中的4个文档中带有"noodles"一词,一切正常,数据库连接和我的节点服务器都正常工作

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

推荐答案

查询正在做的是查找text属性与"noodles"完全匹配的文档.假设您要查询的文档中text属性仅包含 "noodles"的地方,则应改用正则表达式:

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);
    });
});

这篇关于用猫鼬api和nodejs搜索数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆