node.js - Text search in MongoDB, keyword search -


i'm trying keyword search in mongodb database. in mongodb console:

db.logs.find({$text: {$search: 'key1'}}) 

gives correct result. when use mongoose-text-search on nodejs controller error. definition of schema:

var mongoose = require('mongoose'); var schema = mongoose.schema; var textsearch = require('mongoose-text-search');  var log = new schema({     keywords: [string],     description: string,     videoid: string,     logid: string,     date: date,     robot: string });  log.plugin(textsearch); log.index({ keywords: 'text' });  module.exports = mongoose.model('log', log); 

and in controller, @ point do:

log.textsearch('key1', function(err,output) {     if (err){         res.send(500,err);     } else {         console.log(output);     }            }); 

and response is:

{"name":"mongoerror","message":"no such command: text","ok":0,"errmsg":"no such command: text","code":59,"bad cmd":{"text":"logs","search":"key1"}} 

now, message alone make think text search not working, showed before. i'm running mongodb shell version 3.0.2 text search enabled default.

for reason having hard time following moongose's documentation... turns out, easy in normal query (no need plugin). give idea of how can done (not actual code 1 used test idea)

router.get('/keywordsearch', function (req,res) {          var query = log.find();          var findkeywords = [];         if (req.query.keywords != '')         {             var words = req.query.keywords.split(",");              (ii=0; ii<words.length; ii++)                 findkeywords[ii] = {keywords: words[ii].trim()};              console.log(findkeywords);             query.or(findkeywords);         }          query.exec(function (err,logs) {             if (err){                 res.send(500,err);             } else {                 req.logslist = logs;                  res.render('searchdb',{user:req.user,logs:req.logslist});             }         });  }); 

which means can keyword search using:

find({keyword: 'blablabla'}) 

and using or logical operator many keywords.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -