0

I'm using mongodb Realm functions and want to run this query for all my collections in the database. I have to write a collection name; otherwise, I get this error:

'(AtlasError) Pipeline can only have no collection if the first stage is $changeStream', error_code: 'MongoDBError'

here is my code:

exports = function (payload) {
  const movies = context.services
    .get('mongodb-atlas')
    .db('subsDB')
    .collection('subtitles');

  let arg = payload.query.arg;
  let found=movies.aggregate([
    {
      $search: {
        index: 'default',
        text: {
          query: arg,
          path: {
            wildcard: '*',
          },
        },
      },
    },
   
  ]);

How can I run this query on all collections in my database?

moemous
  • 159
  • 3
  • 13

1 Answers1

0

Use db.getCollectionNames:

db.getCollectionNames().forEach(function(collname) {
    c=db[collname].aggregate(yourPipeline);
});

Buzz Moschetti
  • 7,057
  • 3
  • 23
  • 33