1
votes

I created a Jobs API now I want to make a search functionality with skills and location. Example, If I search with the skills HTML, CSS, JavaScript and with the particular location then it should display the list of Jobs that matches the query.

JSON Data:

{
    "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, "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", "location": India }, { "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", "location": USA }, { "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", "location": UK }, { "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", "location": Russia }, { "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.", "location": China }

Route.js file

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

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

module.exports = router;

Search.js file

'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 file

'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

2 Answers

0
votes

You can try this

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


 });
0
votes

To make a search with multiple parameters, you can separate it by comma

Jobs.find({location:"wherever",skills:{$in:["Java","Javascript"]}}, function(err, data) {
    //your code
}

The $in parameter takes an array as argument and will return all the documents that contains one of the fields on the array.