【问题标题】:use of $text with mongoose for text search is not working使用 $text 和 mongoose 进行文本搜索不起作用
【发布时间】:2016-05-28 15:22:12
【问题描述】:

我正在尝试使用 $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

请在这里说我做错了什么,谢谢

【问题讨论】:

    标签: mongodb indexing mongoose full-text-search


    【解决方案1】:

    我添加了一些文档后,集合中没有文档,它工作正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-06
      • 2019-11-25
      • 2015-09-18
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多