0

I have a web application where i need to implement search functionality on all the data coming from the Mongodb. I need to combine all the collections and implement search on that.

  var db= client.db('bloggingapp');
    var collections = db.listCollections().toArray(function (err, names){
    const coll = Object.keys(mongoose.connection.collections);
      //console.log(coll);
      for(var i=0;i<coll.length;i++){
        //console.log(coll);
      const allData = db.collection(coll[i]).find({}).toArray(function (err, result) {
          if (err) throw err;
          console.log(JSON.stringify(result, null, 2));  
          return result;
       
    });
    }
  })

I have the complete collection of data in the result json but how do i store it to object so that i can implement my search on that json.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
vana
  • 13
  • 1
  • 6
  • `names` variable has the collection names - that is what you want to iterate on and read data from. – prasad_ Mar 17 '22 at 02:51
  • can you provide some skeleton code – vana Mar 17 '22 at 04:30
  • You can iterate over the collection names and query each collection as in this post: https://stackoverflow.com/questions/70470790/is-there-a-way-to-find-collection-name-from-a-field-name-in-mongo-db-query – prasad_ Mar 17 '22 at 05:00
  • i dont want to run it on mongo shell instead i want to use the generated json to perform a search on web page – vana Mar 17 '22 at 05:02
  • Then, you can use NodeJS driver corresponding APIs to code and run it on the web server. – prasad_ Mar 17 '22 at 05:20
  • i have a get route for each collection. But now i am trying to get all the collections data. Is that possible using mongo – vana Mar 17 '22 at 05:38
  • Please include the entire question in your post - not in comments. So that someone can read the question at one place try to help. – prasad_ Mar 17 '22 at 06:18
  • I dont see any option to edit my question – vana Mar 17 '22 at 16:58

0 Answers0