如何使用各种参数在mongoose中进行搜索查询?

时间:2017-03-24 03:20:08

标签: javascript node.js mongodb search mongoose

我现在创建了一个Jobs API,我希望通过技能和位置来创建搜索功能。例如,如果我使用技能HTML,CSS,JavaScript和特定位置进行搜索,那么它应该显示与查询匹配的作业列表。

JSON数据:

{
    "jobID": 1245,
    "title": "Front-End Developer",
    "description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
    "Skills": "JavaScript, jQuery, Angular.js, React.js, Ember.js",

},
{
    "jobID": 2982,
    "title": "Back-End Developer",
    "description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
    "Skills": "JavaScript, Node.js, Express.js, MongoDB"
},
{
    "jobID": 5782,
    "title": "Full-Stack Developer",
    "description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
    "Skills": "JavaScript, jQuery, Angular.js, backbone.js"
},
{
    "jobID": 4674,
    "title": "Java Developer",
    "description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
    "Skills": "Java, J2EE, JSON, JBOSS, Spring, Hibernate"
},
{
    "jobID": 1589,
    "title": "Hadoop Developer",
    "description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
    "Skills": Hadoop, Spark, Teradata, Scala, Oozie, Java,  etc."
}

{     “jobID”:1245,     “标题”:“前端开发者”,     “描述”:“Lorem ipsum dolor sit amet,consectetuer adipiscing elit.Aenean commodo ligula eget dolor.Aenean massa.Cum sociis natoque penatibus et magnis dis parturient montes,nascetur ridiculus mus。”     “技能”:“JavaScript,jQuery,Angular.js,React.js,Ember.js”,     “位置”:印度 }, {     “jobID”:2982,     “title”:“后端开发者”,     “描述”:“Lorem ipsum dolor sit amet,consectetuer adipiscing elit.Aenean commodo ligula eget dolor.Aenean massa.Cum sociis natoque penatibus et magnis dis parturient montes,nascetur ridiculus mus。”     “技能”:“JavaScript,Node.js,Express.js,MongoDB”,     “位置”:美国 }, {     “jobID”:5782,     “title”:“Full-Stack Developer”,     “描述”:“Lorem ipsum dolor sit amet,consectetuer adipiscing elit.Aenean commodo ligula eget dolor.Aenean massa.Cum sociis natoque penatibus et magnis dis parturient montes,nascetur ridiculus mus。”     “技能”:“JavaScript,jQuery,Angular.js,backbone.js”,     “位置”:英国 }, {     “jobID”:4674,     “title”:“Java Developer”,     “描述”:“Lorem ipsum dolor sit amet,consectetuer adipiscing elit.Aenean commodo ligula eget dolor.Aenean massa.Cum sociis natoque penatibus et magnis dis parturient montes,nascetur ridiculus mus。”     “技能”:“Java,J2EE,JSON,JBOSS,Spring,Hibernate”,     “位置”:俄罗斯 }, {     “jobID”:1589,     “title”:“Hadoop Developer”,     “描述”:“Lorem ipsum dolor sit amet,consectetuer adipiscing elit.Aenean commodo ligula eget dolor.Aenean massa.Cum sociis natoque penatibus et magnis dis parturient montes,nascetur ridiculus mus。”     “技能”:Hadoop,Spark,Teradata,Scala,Oozie,Java等“,     “地点”:中国 }

Route.js文件

'use strict';
var express = require('express'),
    router = express.Router(),
    search = require('../controllers/search');

Router.get('/:query', search.get);

module.exports = router;

Search.js文件

'use strict';
var mongoose = require('mongoose'),
    Jobs = mongoose.model('Jobs');

var search = {

    get: function (req, res) {
        var searchQuery = req.params.query;

        Jobs.findOne({search : searchQuery}, function(err, result) {
            if (err)  throw err;
            console.log(result);
            res.json(result);
        });
    }

};

module.exports = search;

Server.js文件

'use strict';
var express = require('express'),
    app = express();

//Routes
var jobRoute = require('./routes/router');
app.use('/api/jobs', jobRoute);

//Search Routes
var searchRoute = require('./routes/search-route');
app.use('/api/search', searchRoute);

//Server Connection
app.listen(port, function() {
    console.log('Listening on port 8080!');

2 个答案:

答案 0 :(得分:0)

要使用多个参数进行搜索,可以用逗号

分隔
Text='<%#DataBinder.Eval(Container.DataItem, "CompName") %>'

Jobs.find({location:"wherever",skills:{$in:["Java","Javascript"]}}, function(err, data) { //your code } 参数将数组作为参数,并返回包含数组中某个字段的所有文档。

答案 1 :(得分:0)

你可以试试这个

Jobs.find({skill: "html",location:"US"}, function(err, user) 
 {
    if (err)
    {
        res.send(err);
    }
  }


 });