有没有办法编写一个 Nodejs API,使用户能够获取帖子,其中帖子描述包含与用户相关的关键词

问题描述 投票:0回答:1

我正在申请求职。雇主创建职位发布,用户申请该职位。但是,我希望用户获得的职位描述仅包含特定关键字,这些关键字类似于用户在

userModel
下的
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);

雇主发布职位,用户申请职位。职位发布有一个描述元素。如果职位描述中的一个或多个单词与用户 professionalTags 中的任何单词匹配或相似,则该职位发布将自动呈现在用户的获取职位页面上。 这是工作模型

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

如何编写一个 API 来获取职位描述中包含与用户的

professionalTags
类似的单词或单词的职位信息。也就是说,如果用户
professionalTags
有“医学”、“注射”、“手术”、“医院”等词,那么用户应该得到一个描述如下的帖子:

‘您好,我们正在寻找一位拥有医学和外科硕士学位的专业医生’

javascript node.js mongoose api-design
1个回答
0
投票

在 mongodb 中使用

$regex

示例

select * from Job where description like %hospital%

在蒙戈

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

参考:如何在猫鼬上使用“LIKE”运算符?

希望这有效...

© www.soinside.com 2019 - 2024. All rights reserved.