【问题标题】:How to search keywords from MongoDB using NodeJS express framework (Error: MongoDB schema.index is not a function)如何使用 NodeJS express 框架从 MongoDB 中搜索关键字(错误:MongoDB schema.index 不是函数)
【发布时间】:2021-08-26 01:05:29
【问题描述】:

我正在尝试使用 NodeJS express 框架搜索 MongoDB 文档中的关键字。我正在使用 mongoose 的搜索文本,但出现“错误”:“Minutes.index 不是函数”

架构:

const MinutesSchema = new Schema({  
            
    minute_id :{
        type: Number,
        required: true
    },

    minute_type :{ 
        type: String, 
        default: "",
    },
        
    description :{ 
        type: String, 
        default: ""
    },
  
}, {collection:"pm_meeting_minutes"});

const Minutes = mongoose.model('pm_meeting_minutes', MinutesSchema);

exports.Minutes = Minutes;

//这是我在我的模型函数中尝试过的

exports.getSearch = () => {
 
   Minutes.index({ minute_type: "text"});
   
   return Minutes.find( { $text: { $search: "Issue" } } )

}

//我不断得到的错误是

“错误”:“Minutes.index 不是函数”

//预期输出

从 MongoDB 中获取所有包含 minute_type="Issue" 的文档

请帮我调试代码。

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    替换

    const Minutes = mongoose.model('pm_meeting_minutes', MinutesSchema);
    

    exports.Minutes = mongoose.model('pm_meeting_minutes', MinutesSchema);
    

    并在结束行删除导出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-19
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 2020-12-28
      • 2020-07-10
      相关资源
      最近更新 更多