mongoose-text-search无效

时间:2016-04-14 08:04:00

标签: node.js mongodb mongoose

我在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;

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'
});

1 个答案:

答案 0 :(得分:-1)

我明白了,这是mongoose连接的问题,当我运行查询时它的状态是连接没有连接;所以,我把它改成了以下,它起作用了。

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