使用带有mongoose的$ text进行文本搜索不起作用

时间:2016-02-17 04:26:01

标签: mongodb indexing mongoose full-text-search

我正在尝试使用$ text进行文本搜索,如下所示

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyparser = require('body-parser');
var port = process.env.PORT || 8000;
var router = express.Router();
var cors = require('cors');
var Schema = mongoose.Schema;
var router = express.Router();
var exec = require('exec');
app.use(bodyparser.json());

//establish the connection
var db=mongoose.connect('mongodb://localhost/book_publisherSearch');


//define the schema
var authorSchema = new mongoose.Schema(
    {
        authorId : Number,
        Description : String,
        firstName : String
    });

authorSchema.index({ firstName: 'text'});


var Authors= mongoose.model('Authors',authorSchema);

router.route('/search')
    .get(function(req,res){
        Authors.find({ $text : { $search : "Bharath" }},function(err,authors){
            console.log("inside search");
            res.send(authorss);
        })
    })


app.use(router);
app.listen(port);

但是当我遇到错误时

c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\base.js:246
        throw message;      
              ^
    TypeError: Cannot read property 'length' of undefined
        at processResults (c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1581:31)
        at c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1619:20
        at c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1157:7
        at c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1890:9
        at Server.Base._callHandler (c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\base.js:448:41)
        at c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:481:18
        at MongoReply.parseBody (c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\responses\mongo_reply.js:68:5)
        at null.<anonymous> (c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:439:20)
        at emit (events.js:107:17)
        at null.<anonymous> (c:\Users\zendynamix\IdeaProjects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:201:13)

    Process finished with exit code 1

在数据库中我有一个Authors集合,其中包含少量包含名字的集合有Bharath

请说出我在做错了什么,谢谢

1 个答案:

答案 0 :(得分:2)

一旦我添加了一些工作正常的文件,集合中就没有文件