javascript - 无法规范化查询 : IndexNotFound: text index required for $text query', 代码:17287

标签 javascript mongodb mean

错误是在下面的 Product.find 语句中触发的:

var bodyparser = require('body-parser');
var express = require('express');
var status = require('http-status');
var _ = require('underscore');
var mongoose = require('mongoose');
var productSchema = require('./product');

var schema = new mongoose.Schema(productSchema);

schema.index({ name: 'text' });

module.exports = function(wagner) {
var api = express.Router();

 api.use(bodyparser.json());

 api.get('/product/text/:query', wagner.invoke(function(Product) {
 return function(req, res) {
  console.log("we are  in the get " + req.params.query);
  Product.
    find(
      { $text : { $search : req.params.query } },
      { score : { $meta: 'textScore' } }).
    sort({ score: { $meta : 'textScore' } }).
    limit(10).
    exec(handleMany.bind(null, 'products', res));
  };
}));

return api;
};
function handleMany(property, res, error, result) {
 console.log("We are handling the many");
 if (error) {
  console.log(error);
  return res.
  status(status.INTERNAL_SERVER_ERROR).
  json({ error: error.toString() });
 }

 var json = {};
 json[property] = result;
 res.json(json);
}

我在 Windows 10 上运行 MongoDB 3.4.2。我在 MongoDB shell 中明确运行了语句 db.products.ensureIndex({name: "text"});时间我没有收到错误。但是,当查询时间超过 2000 毫秒时,它仍然会间歇性地出现超时错误。我认为我不必在 MongoDB shell 中显式添加索引,因为我在上面的代码中添加了架构索引,但我不确定。

谢谢

威廉

最佳答案

我进入 MongoDB shell 并将索引放在产品集合上,如下所示:

db.products.createIndex({name: "text"})

这是我的解决方案,它有效,但我不知道是否存在某个地方的故障,使得该解决方案成为必要。

关于javascript - 无法规范化查询 : IndexNotFound: text index required for $text query', 代码:17287,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44031415/

相关文章:

javascript - 谷歌地图API V3 : How to disable vegetation?

javascript - 如何在向下滚动时平滑地降低 div 的高度并在向上滚动时以相同的速率增加它?

MEAN.JS : How to create of 1 model that requires another

mongodb - 从请求中序列化一个表单并放入 mongo

python - 使用 Python 用该列的平均值减去数据框中的每一列

javascript - angularjs 预检响应具有无效的 HTTP 状态代码 404

javascript - 如何解决跨域请求被阻塞?

javascript - VSCode 不会对不同根文件夹中的类执行智能感知

MongoDB:如何通过子文档 ID 查找?

mongodb - 奇怪的 Mongoose 。我从 findOne 的文档中得到了废话。