I am making a job finder application. The employer creates a job post and users apply for the job. However, I want users to get job posts whose description only contains specific keywords that are similar to tags that a user has in their userModel under professionalTags.

const mongoose = require('mongoose');
const validator = require('validator');
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken');
const crypto = require('crypto');

const userSchema = new mongoose.Schema({
   username : {
       type : String,
       required : [true, 'Please enter username'],
       maxlength: [30, 'Your name cannot exceed 30 characters']
   },
   email : {
       type : String,
       required : [true, 'Please enter your email address'],
       unique : true,
       validate : [validator.isEmail, 'Please enter valid email address']
   },
   professionalTags: {
       type: String,
       required: [false, 'Please enter yourncustom words'],
   },
   phoneNo: {
       type: String,
       required: false
   },
   role : {
       type : String,
       enum : {
           values : ['user', 'employer', 'admin'],
           message : 'Please select correct role'
       },
       default : 'user'
   },
   password : {
       type : String,
       required : [true, 'Please enter password for your account'],
       minlength : [4, 'Your password must be at least 4 characters long'],
       select : false
   },
   createdAt : {
       type : Date,
       default : Date.now
   },
   resetPasswordToken : String,
   resetPasswordExpire : Date

});

// Encrypting password before saving user
userSchema.pre('save', async function (next) {
   if(!this.isModified('password')) {
       next()
   }
   this.password = await bcrypt.hash(this.password, 11)
})

// Compare user password
userSchema.methods.comparePassword = async function (enteredPassword) {
   return await bcrypt.compare(enteredPassword, this.password)
}

// Return JWT token
userSchema.methods.getJwtToken = function () {
   return jwt.sign({ id: this._id }, process.env.JWT_SECRET, {
       expiresIn: process.env.JWT_EXPIRES_TIME
   });
}


// Generate password reset token
userSchema.methods.getResetPasswordToken = function () {
   // Generate token
   const resetToken = crypto.randomBytes(20).toString('hex');

   // Hash and set to resetPasswordToken
   this.resetPasswordToken = crypto.createHash('sha256').update(resetToken).digest('hex')

   //set token expire time
   this.resetPasswordExpire = Date.now() + 30 * 60 * 1000

   return resetToken
}



module.exports = mongoose.model('User', userSchema);

The Employer posts a job and the users apply for jobs. The job post has a description element. If a word or words in the job description match or are similar to any word in the users professionalTags, then the job post automatically is rendered on the user's get jobs page. Here is the jobs model

const mongoose = require('mongoose')

const jobSchema = new mongoose.Schema({

   description: {
       type: String,
       required: [true, 'Please decribe what you want'],
   },
   images: [
       {
           public_id: {
               type: String,
               required: true,
           },
           url: {
               type: String,
               required: true,
           },
       }
   ],
   numOfjobReactions: {
       type: Number,
       default: 0
   },
   jobReactions: [
       {
           user: {
               type: mongoose.Schema.Types.ObjectId,
               required: true,
               ref: 'User'
       
           }, 
           username: {
              type: String,
              required: true, 
           },
          comment: {
           type: String,
           required: true
          } 
       }
   ], 
   user: {
       type: mongoose.Schema.ObjectId,
       ref: 'User',
       required: true
   }, 
   createdAt: {
       type: Date,
       default: Date.now
   }
})

module.exports = mongoose.model('Job', jobSchema);

How do I write an API to get job posts whose description has a word or words similar to those in the user's professionalTags. That is, if the users professionalTags has words like Medicine, injection, surgery, hospital, then the user should get a post whose description is like this:

'Hello, we are looking for a professional doctor who has a master's degree in Medicine and surgery'

1

There are 1 best solutions below

0
karan On

use $regex in mongodb

example

select * from Job where description like %hospital%

in mongo

var descName="hospital";
models.jobSchema.find({ "description": { $regex: '.*' + descName + '.*' } },
    function(err,data) {
        console.log('data',data);
});

Hope this works...