猫鼬多字段选择不起作用

时间:2020-02-24 15:20:55

标签: node.js mongodb mongoose

在以下模式下,我尝试使用选定的字段进行查找。结果,我只收到前2个org_name和description,但其他人未收到。任何想法。我是猫鼬的新手。我也无法从文档中找到任何原因。任何帮助将不胜感激。

我的模式:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let JobsSchema = new Schema ({
    _id: {type:String, required: true, max:100},
    sector: {type:String, required: true, max:100},
    category: {type:String, required: true, max:100},
    apply_mode: {type:String, required: true, max:100},
    org_name: {type:String, required: true, max:100},
    description: {type:String, required: true},
    start_date: {type: Date},
    end_date: {type: Date},
    total_openings: {type: Number},
    total_openings_description: {type:String},
    min_salary: {type: Number},
    max_salary: {type: Number},
    salary_description: {type: String},
    apply_steps: {type: String},
    tags: [],
    additional_dates: [{
        additional_date_label: {type:String, required: true, max:100},
        additional_date_value: {type:Date},
    }],
    age_limits: [{
        age_limit_label: {type:String, required: true, max:100},
        age_limit_min_value: {type: Number},
        age_limit_max_value: {type: Number},
    }],
    application_fees: [{
        application_fee_label: {type:String, required: true, max:100},
        application_fee_value: {type: Number},
    }],
    url_links: [{
        url_link_label: {type:String, required: true, max:100},
        url_link_value: {type:String, required: true, max:100},
    }]
},
{ collection: 'Jobs' }); // this line to match name in db

module.exports = mongoose.model('jobs', JobsSchema);

我的查询:

Job.find(
  {},
  "org_name description start_date end_date total_openings min_salary max_salary",
  { sort: { start_date: "asc" }, skip: pageNumber * pageSize, limit: pageSize },
  (err, jobs) => {
    if (err) {
      res.render("error", {
        errMsg: "issue in getting records. Try after sometime.",
        errBody: err
      });
    }

    if (jobs) {
      res.render("listJob", {
        jobs: jobs
      });
    }
  }
);

结果

[
  {
    "_id": "Job1",
    "org_name": "Test org",
    "description": "<p>Test Desc</p><p><b>Hello world</b></p>"
  },
  {
    "_id": "Job2",
    "org_name": "Test org11",
    "description": "<p>Test descr</p>"
  },
  {
    "_id": "Job10",
    "org_name": "Test inc 1",
    "description": "<p>,smdfndfskj</p>"
  }
]

1 个答案:

答案 0 :(得分:0)

我最终发现,投影字段应该在所有文档中都存在,然后才适用。