Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MongoDB中进行搜索查询_Mongodb_Search_Searchqueryset - Fatal编程技术网

如何在MongoDB中进行搜索查询

如何在MongoDB中进行搜索查询,mongodb,search,searchqueryset,Mongodb,Search,Searchqueryset,我在一个名为profiles的集合中拥有profiles。我正在根据用户进行查询以搜索配置文件 提供的字段。搜索表单(UI表单),用户有不同的选项来搜索配置文件,如“女性”或“男性”或“任何”,然后单击搜索按钮。这样,用户可以通过提供以下字段的组合来搜索我的收藏配置文件 性别 国家 城市 婚姻状况 教育 宗教信仰 所以,例如,用户选择的字段为:性别:'女性',国家:'巴基斯坦',城市:'伊斯兰堡',婚姻状况:'任何',教育:'任何',宗教:'伊斯兰',那么MongoDB的查询将是什么 我需要

我在一个名为profiles的集合中拥有profiles。我正在根据用户进行查询以搜索配置文件 提供的字段。搜索表单(UI表单),用户有不同的选项来搜索配置文件,如“女性”或“男性”或“任何”,然后单击搜索按钮。这样,用户可以通过提供以下字段的组合来搜索我的收藏配置文件

  • 性别
  • 国家
  • 城市
  • 婚姻状况
  • 教育
  • 宗教信仰
所以,例如,用户选择的字段为:性别:'女性',国家:'巴基斯坦',城市:'伊斯兰堡',婚姻状况:'任何',教育:'任何',宗教:'伊斯兰',那么MongoDB的查询将是什么

我需要有一个所有案件的动态查询,请

到目前为止,我试过:

Profile.find(
    {
        gender: req.body.gender,
        country: req.body.country,
        city: req.body.city,
        maritalStatus: req.body.maritalStatus,
        education: req.body.education,
        religion: req.body.religion
    },
    {},
    function(err, profiles){
        if(profiles!=null)
            { res.status(200).json({status: true, message:'Profile(s) found', profiles})}
        else
            {res.status(404).json({status: false, message:'No profile(s) found.'})}
    }
);

但上述查询并非对所有情况都是动态的。

只需提取
req.body
对象中存在的字段,并将其添加到对象中即可。此对象将用于筛选查询结果

const filter = Object.entries(req.body)
                     .reduce((acc, curr) => (acc[curr[0]] = curr[1], acc), {});
最后,您将拥有一个
过滤器
对象,该对象将仅包含用户选择的那些属性。您可以在查询中传递此筛选器对象

Profile.find(filter, {}, function(err, profiles) {
    if(profiles!=null)
          return res.status(200).json({status: true, message:'Profile(s) found', profiles});

    res.status(404).json({status: false, message:'No profile(s) found.'});
});

只需提取
req.body
对象中存在的字段,并将它们添加到对象中。此对象将用于筛选查询结果

const filter = Object.entries(req.body)
                     .reduce((acc, curr) => (acc[curr[0]] = curr[1], acc), {});
最后,您将拥有一个
过滤器
对象,该对象将仅包含用户选择的那些属性。您可以在查询中传递此筛选器对象

Profile.find(filter, {}, function(err, profiles) {
    if(profiles!=null)
          return res.status(200).json({status: true, message:'Profile(s) found', profiles});

    res.status(404).json({status: false, message:'No profile(s) found.'});
});

感谢您的快速回答,这让我明白了一件关于“任何”案例的事情,将该字段从查询中排除。但我很抱歉这不起作用。你说不起作用是什么意思?这样,您将根据请求正文中传递的值查询数据库。它们可以是
档案
收藏的字段,也可以是一些完美的字段。响应状态需要修改条件,比如:if(profiles.length>0),因为它将返回一个概要文件数组。谢谢。谢谢你的快速回答。通过从查询中排除该字段,这让我清楚了一件关于“任何”情况的事情。但我很抱歉这不起作用。你说不起作用是什么意思?这样,您将根据请求正文中传递的值查询数据库。它们可以是
档案
收藏的字段,也可以是一些完美的字段。响应状态需要修改条件,比如:if(profiles.length>0),因为它将返回一个概要文件数组。非常感谢。