【问题标题】:mongoose-text-search is not working猫鼬文本搜索不起作用
【发布时间】:2016-08-05 15:41:24
【问题描述】:

我已经在 nodejs 上构建了一个旧项目,我正在使用 MongoDb 来存储数据。 MongoDB版本是2.4

问题是我正在对集合运行文本搜索,但它从不返回任何内容,这意味着 console.log(arguments);永远不要打印任何东西

通知你们,我可以在 mongo 终端上运行 db.socialposts.runCommand( "text", { search: "accessories",limit: 1 } ) 并很快得到结果

var options, search;

options = {
  limit: 1
};

search = 'accessories';

console.log(search);

SocialPost.textSearch(search, options, function(err, out) {
 console.log(arguments);
 return true;
});

它的架构是 var 架构;

schema = new mongoose.Schema({
entity_id: {
    type: mongoose.Schema.Types.ObjectId,
    required: true
},
social_id: {
  type: String,
  required: true
},
type: {
  type: String,
  required: true
},
app: {
  type: String,
  required: true
},
date: {
  type: Date,
  required: true
},
url: {
  type: String,
  required: true
},
post: {
  type: String
},
title: {
  type: String
},
image: {
  type: String
},
video: {
 type: String
},
hashtags: [String]
}, {
 strict: 'throw'
});

而索引是

schema.index({
  type: 1,
  social_id: 1
});

schema.index({
  type: 1,
  entity_id: 1,
  date: 1
});

schema.index({
  title: 'text',
  post: 'text'
});

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    我想通了,是猫鼬连接的问题,当我运行查询时,它的状态是连接未连接;所以,我把它改成了following,它工作了。

      mongoose.connect('mongodb://localhost/test', function (err) {
          ItemModel.textSearch('D', function (err, output) {
            if (err)
              console.log(err);
            else
              console.log(output);
            mongoose.disconnect();
         });
        }); 
    

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 1970-01-01
      • 2016-03-27
      • 2016-05-17
      • 2011-07-28
      • 1970-01-01
      • 2014-11-17
      • 2020-03-16
      相关资源
      最近更新 更多