db.getCollectionInfos is not a function

Viewed 869

I am writing a MERN app and am trying to get all the collection names in my MongoDB database using the db.getCollectionInfos method. However, I'm getting the following error:

Error: db.getCollectionInfos is not a function

Here is my code. The database is connected, as it is connected and returns document data with other routes I have in my app.

Can someone help me figure out what I'm doing wrong?

  mongoose.connect(process.env.DATABASE, { useNewUrlParser: true });
  const db = mongoose.connection;

  app.post("/fetchdatabasecollections", (req, res) => {
    let regex = /someRegEx/g;
    let collections = db.getCollectionInfos({ name: { $regex: regex } });
  })
2 Answers

getCollectionInfos is mongo shell method and is not provided by nodejs driver. Use listCollections instead

const filter = { name: { $regex: regex } };
const collectionInfos = await mongoose.connection.db.listCollections(filter).toArray();

Try this bro..

var search_something = 'abcd';
var collections = db.getCollection('YOUR_TABLE').find( {
    name: new RegExp( '^' + search_something ),
} );

Related