猫鼬中的对象数组的部分搜索

时间:2020-04-10 15:39:38

标签: node.js mongodb search mongoose

在猫鼬的对象数组中进行部分搜索时,我遇到了一些错误。谁能帮助我摆脱这个问题。

猫鼬模型

const mongoose = require('mongoose');

const eUserSchema = new mongoose.Schema({
  fname: {
    type: String,
  },
  lname: {
    type: String,
  },
  email: {
    type: String,
  },
  education: [
    {
      eduInstitute: String,
      degree: String,
      eduFrom: String,
      eduTo: String,
    },
  ],
});

exports.eUserModel = mongoose.model('expertUser', eUserSchema);

部分搜索查询

let searchOptions = {
  $regex: req.query.search,
  $options: 'i',
};
let searchKey = {
  $or: [
    { fname: searchOptions },
    { lname: searchOptions },
    { email: searchOptions },
    {
      education: [
        {
          eduInstitute: searchOptions,
          degree: searchOptions,
          eduFrom: searchOptions,
          eduTo: searchOptions,
        },
      ],
    },
  ],
};
searchedExperts = await eUserModel.find(searchKey);

我收到此错误

(node:17222) UnhandledPromiseRejectionWarning: CastError: Cast to String failed for value "{ '$regex': 'myname', '$options': 'i' }" at path "eduInstitute" for model "expertUser"

任何人都可以帮助我发现问题。我无法在教育字段中进行部分搜索。

0 个答案:

没有答案