How to do full text search in all collections in mongodb without specifying the collection name

15 views
Skip to first unread message

Rigin Oommen

unread,
Sep 27, 2016, 8:29:34 AM9/27/16
to Mongoose Node.JS ODM
var express = require('express');
var router = express.Router();

var mongoose = require('mongoose');

var db = mongoose.connection;

router
.param('params', function(req, res, next, params) {
  mongoose
.connection.db.collectionNames(function (err, names) {
         
var searchresults = [];
       
for(var index in names) {
           
var collectionLength = (names.length)-1;
           
var collectionName = names[index].name;
            db
.collection(collectionName).find({$text: {$search: params}}).toArray(function(err, results) {
               
if(results !== null) {
                    searchresults
.push(results);
               
}
               
if(collectionLength == index) {
                    console
.log(index);
                    req
.request = searchresults;
                   
return next();
               
} else    {
                    console
.log("Loop Continues")
               
}
           
});
       
}
   
});

});

router
.get('/search/:params', function (req, res, next) {
    res
.json(req.request);
});


How to do the full text search and get combined results in a single response




Reply all
Reply to author
Forward
0 new messages